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) ...
随机推荐
- java - BigDecimal的format()方法和setScale()方法格式字符串
1.BigDecimal.setScale()方法用于格式化小数点 setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOWN)直接删 ...
- leetcode 162 Find Peak Element(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Convolutional Neural Networks for Visual Recognition 8
Convolutional Neural Networks (CNNs / ConvNets) 前面做了如此漫长的铺垫,现在终于来到了课程的重点.Convolutional Neural Networ ...
- freeMarker(五)——模板开发指南补充知识
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南补充知识 1. 自定义指令 自定义指令可以使用 macro ...
- 系列文章--AJAX技术系列总结
各种AJAX方法的使用比较 用ASP.NET写个SQLSERVER的小工具 写自己的ASP.NET MVC框架(下) 写自己的ASP.NET MVC框架(上) 用Asp.net写自己的服务框架 ...
- 反编译工具Reflector下载(集成FileGenerator和FileDisassembler)
Reflector是一款比较强大的反编译工具,相信很多朋友都用过它,但reflector本身有很多局限性, 比如只能一个一个的查看方法等,但幸好reflector支持插件功能目前网上有很多reflec ...
- Sentry的要点
1.Apache的Build 在研究Sentry的时候,发现没有bin.jar,只能手工编辑工程,但是编辑发现很多jar包有问题:在访问官网的时候发现左侧菜单中有一项是builds,点开后(https ...
- Poj_1004_FinancialManagement
一.Description Larry graduated this year and finally has a job. He's making a lot of money, but someh ...
- UVa11624(逃离火焰问题)
#include"cstdio" #include"queue" #include"cstring" using namespace std ...
- netty支持的协议
流经网络的数据总是具有相同的类型:字节.这些字节是如何流动的主要取决于我们所说的 网络传输--一个帮助我们抽象底层数据传输机制的概念.用户并不关心这些细节:他们只想确保他们的字节被可靠地发送和接收. ...