题目:http://poj.org/problem?id=2488

题目大意:可以从任意点开始,只要能走完棋盘所有点,并要求字典序最小,不可能的话就impossible;

思路:dfs+回溯,因为字典序最小,如果可以的话,肯定是从(1,1)开始的。然后递归搜索该点的所有方向,不能满足就回溯,直到找到能满足的,或者一直找不到。

代码+注释:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std; bool vis[27][27];
int des[8][2] = {-2, -1, -2, 1, -1, -2, -1, 2, 1, -2, 1, 2, 2, -1, 2, 1};//八个方向 int T, n, m, tempx, tempy; struct node{
int x;
int y;
}stu[27]; bool flag;
bool judge(int x, int y) {
if(x < 1 || x > n || y < 1 || y > m)
return false;
if(vis[x][y])
return false;
if(flag)
return false;
return true;
} void dfs(int x, int y, int num) {
stu[num].x = x;
stu[num].y = y;
if(num == m * n) {//跑完所有的点了
flag = true;
return;//这里的return 不是直接出去,而是返回上一状态?我的理解
}
for(int i =0; i < 8; i++) {//遍历所有的方向
tempx = x + des[i][0];
tempy = y + des[i][1];
if(judge(tempx, tempy)) {//这个点满足条件
vis[tempx][tempy] = true;//标记访问过
dfs(tempx, tempy, num + 1);//继续搜索,点数+1
vis[tempx][tempy] = false;//说明该点虽然满足条件,但是无法走完全部的点,因此回溯
}
}
} int main() {
scanf("%d", &T);
int j = 1;
while(T--) {
scanf("%d%d", &m, &n);
flag = false;
memset(vis, false, sizeof(vis));
vis[1][1] = true;//肯定是从第一个开始的(保证字典序最小)
dfs(1, 1, 1);// (1,1,第一个点)
printf("Scenario #%d:\n", j++);
if(flag) {
for(int i = 1; i <= n * m; i++) {
printf("%c%c", 'A' + stu[i].x - 1, '1' + stu[i].y - 1);
}
cout << endl;
} else cout << "impossible" << endl;
cout << endl;
}
}

POJ2488 A Knight's Journey的更多相关文章

  1. 快速切题 poj2488 A Knight's Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31195   Accepted: 10 ...

  2. poj2488 A Knight's Journey裸dfs

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12 ...

  3. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  4. poj-2488 a knight's journey(搜索题)

    Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...

  5. poj2488 A Knight's Journey

    http://poj.org/problem?id=2488 题目大意:骑士厌倦了一遍又一遍地看到同样的黑白方块,于是决定去旅行. 世界各地.当一个骑士移动时,他走的是“日”字.骑士的世界是他赖以生存 ...

  6. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  7. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  8. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  9. HDOJ-三部曲一(搜索、数学)- A Knight's Journey

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

随机推荐

  1. FFMPEG相关开源项目

    1.FFmpeg build for android random architectures with example jnihttps://github.com/appunite/AndroidF ...

  2. Gym - 100513B:Colored Blankets (构造)(存疑)

    题意:给定N的棒棒,K种颜色,每个棒棒的两端可以涂色.现在已知所有的线段要么有一端涂色,要么两端都没有涂色,现在要求把所有的没涂色的部分涂色,使得我们可以把涂色后的棒棒分为N/K组,每组的涂色情况相同 ...

  3. jqgrid--api,官网demo,编辑

    api参考: http://blog.csdn.net/hurryjiang/article/details/7551477 官网demo: http://www.trirand.com/blog/j ...

  4. BZOJ1636&&1699:[USACO2007JAN]Balanced Lineup

    浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  5. eclipse如何集成tomcat插件

    刚在Eclipse官方网站下载的Eclipse,需要自己手动安装tomcat插件才能配置tomcat服务器.网上好多安装方法,这里我就不厌其烦的再写一遍,边安装边截图讲解下安装方法. 首先你要去下载E ...

  6. 解析CmdLine参数

    UBOOL ParseParam( const TCHAR* Stream, const TCHAR* Param, UBOOL bAllowQuoted ) { const TCHAR* Start ...

  7. Ruby中的并行赋值和嵌套赋值

    一. Ruby 的赋值实际是以并行方式执行的,所以赋值语句右边的值不受赋值语句本身的影响.在左边的任意一个变量或属性赋值之前,右边的值按他们出现的顺序被计算出来. 1.当赋值语句有多于一个左值时,赋值 ...

  8. hibernate 数据关联一对多

    一对多,多对一 (在多的一端存放一的外键) 但是在实体类中不需要创建这个外键 // 在一的一方创建Set集合 public class User { private Integer id; priva ...

  9. python中报中文编码异常,Non-ASCII ,but no encoding declared

    异常信息: SyntaxError: Non-ASCII character '\xe5' in file a.py on line 9, but no encoding declared; see ...

  10. Java学习路线-知乎

    鼬自来晓 378 人赞同 可以从几方面来看Java:JVM Java JVM:内存结构和相关参数含义 · Issue #24 · pzxwhc/MineKnowContainer · GitHub J ...