POJ2488 A Knight's Journey
题目: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的更多相关文章
- 快速切题 poj2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31195 Accepted: 10 ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- poj-2488 a knight's journey(搜索题)
Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...
- poj2488 A Knight's Journey
http://poj.org/problem?id=2488 题目大意:骑士厌倦了一遍又一遍地看到同样的黑白方块,于是决定去旅行. 世界各地.当一个骑士移动时,他走的是“日”字.骑士的世界是他赖以生存 ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- 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 ...
- HDOJ-三部曲一(搜索、数学)- A Knight's Journey
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
随机推荐
- vs参数配置
配置属性-常规: 输出目录:工程的输出目录,主要包括.exe..dll..lib文件,是工程最后想要的文件.vs2015位于解决方案的\x64\Debug下,vs2010,vs2005位于解决方案的D ...
- bzoj 1007: [HNOI2008]水平可见直线 半平面交
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=1007; 题解 其实就是求每条直线的上半部分的交 所以做裸半平面交即可 #include ...
- Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...
- Azure自动化部署服务 (1)
Azure中已经发布了自动化部署服务的PaaS功能. 本文将介绍自动化服务Automation初始化过程. 在Azure Management Portal上左边可以看到Azure的各种服务,其中一项 ...
- 面向对象(Java中普通代码块,构造代码块,静态代码块区别及代码示例)
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...
- linux命令-vim一般模式下光标移动
vim 有一般模式,编辑模式,命令模式 ///////一般模式可以光标移动,复制,剪切,粘贴 编辑模式可以输入想输入的字符 命令模式刚才用到了set nu //////////// ...
- BAT小米奇虎美团迅雷携程等等各大企业校招,笔试面试题。
类似在线测试的方式展示题目. 历年在线笔试试卷: 百度 http://www.nowcoder.com/paper/search?query=%E7%99%BE%E5%BA%A6 腾讯http:// ...
- svn merge和branch 详解
1.本地Repository的创建 repository的创建很简单,假设我要在D:\TortoiseSVN\TestRepository目录中创建repository,只需右键TestReposit ...
- 【Redis】Redis事务详解,Redis事务支持回滚(不支持悲观锁)
1.redis事物参考:https://baijiahao.baidu.com/s?id=1613631210471699441&wfr=spider&for=pc (php操作red ...
- Struts2学习第七课 ActionSupport
com.opensymphony.xwork2.ActionSupport类是默认的Action类,如果某个Action节点没有配置class属性,则ActionSupport即为待执行的Action ...