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) ...
随机推荐
- 06 - Django应用第三步
知识点 1) 编写urls 配合include()的URL查找过程 获取正则抓取的值并命名, 给url取名 2) 模板的编写 for循环的遍历 用点的方式执行函数, 不带括号 3) 视图函数的编写 H ...
- 2.4 AppDelegate 的 3 个生命周期
Classed/AppDelegate.cpp 文件内容如下: #include "cocos2d.h" #include "CCEGLView.h" #inc ...
- 【leetcode刷题笔记】Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- freeMarker(四)——模板开发指南之模板
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南之模板 1. 总体结构 实际上用程序语言编写的程序就是模板 ...
- tarjan求割点
首先给大家一个网址讲的比较细:http://www.cnblogs.com/en-heng/p/4002658.html 如果还有不懂的话,可以回来再看看我的文章; 概念明确: 树边:(在[2]中称为 ...
- NOIp2018集训test-10-17 (bike day3)
发现自己gradully get moodier and moodier了 负面情绪爆发地越来越频繁,根本out of control,莫名其妙地就像着了魔一样 为什么用英语大概是因为今天早上早自习因 ...
- 原来问题在这里-我的memory leak诊断历程
自从公司开始将java作为主要开发语言后,C++与java的混合应用日趋增多. java与C++的通信主要也是使用JNI来完成,这并没有什么问题.对于这样的混合应用项目来说,最大的噩梦莫过于memor ...
- ie11
可用:document.documentMode来检测. var isIE11 = function() { var result = false; if (document.documentMode ...
- 【转】Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout
相对布局:RelativeLayout RelativeLayout也是非常常用的布局,能够精确对控件的位置进行网格对齐,可以设置在控件与其他控件的相对位置,以及控件在容器中的位置.缺省控件的位置为最 ...
- 人物-IT-柳传志:柳传志
ylbtech-人物-IT-柳传志:柳传志 柳传志,英文名:Chuanzhi Liu,男,汉族,中共党员,1944年4月出生于江苏镇江,联想控股股份有限公司董事长,联想集团创始人. 企业家.投资家.全 ...