poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488
Sample Input
3
1 1
2 3
4 3
Sample Output
Scenario #1:
A1 Scenario #2:
impossible Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4

题目:给你一个p*q的棋盘,规则布局参考上图。列用字母表示,行用数字表示。这样一个左上角的节点就是(A,1)。
骑士的棋子走日字形状,可以从任意节点出发,终点也是任意的。问你能不能遍历所有的位置,并输出这样的路径,如果
存在多条路径,输出字典序最小的那条路径。
分析:按照上面那个图片的提示,从当前位置出发遍历8个方向,如果最后能到达,得到的就是字典序最小的序列。
起点一定会是(A1)。直接从A1开始dfs。
通过这道题,再次体会到回溯的地方。。。。。。
code:
#include<stdio.h>
#include<string.h>
#include <queue>
#include <stack>
#include <algorithm>
#define Max 21 using namespace std; int n, m;
bool vis[30][30];
int x[]={-1, 1, -2, 2,-2, 2,-1, 1};
int y[]={-2,-2, -1,-1, 1, 1, 2, 2};
//从当前马的位置可以走到的8个位置
//在满足字典序最小的前提下 这8个方向的遍历先后不能变动
bool ok; struct node
{
int col, row;
}path[1000];
int e=0; bool sure(int i, int j)
{
if(i>=1 && i<=n && j>=1 && j<=m)
return true;
else
return false;
} void dfs(int i, int j, int cnt)//cnt表示我们在当前这次dfs偶中搜索到的节点
{
path[cnt].col=j; path[cnt].row=i;//当前位置进入路径队列 if(cnt==n*m){
ok=true; return; //所有节点都已经遍历到
}//在这return 开始往回回溯 int a, b;
for(int k=0; k<8; k++){ a=i+x[k];
b=j+y[k];
if(sure(a, b) && !vis[a][b]){
vis[a][b]=true;
dfs(a, b, cnt+1);
if(ok==true) return;//在这return 返回主函数
vis[a][b]=false;
}
}
return;
} int main()
{
int tg; scanf("%d", &tg);
int cnt=1; while(tg--)
{
scanf("%d %d", &n, &m);
ok=false;
if(n==1 && m==1){
printf("Scenario #%d:\n", cnt++);
printf("A1\n\n");
continue;
}
else{
ok=false;
memset(vis, false, sizeof(vis));
vis[1][1]=true;
e=0;
dfs(1, 1, 1); if(!ok){
printf("Scenario #%d:\n", cnt++);
printf("impossible\n\n");
}
else{
printf("Scenario #%d:\n", cnt++);
for(int i=1; i<=n*m; i++)
{
printf("%c%d", path[i].col+'A'-1, path[i].row );
}
printf("\n\n");
}
}
}
return 0;
}
poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】的更多相关文章
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- POJ 2488 A Knight's Journey (回溯法 | DFS)
题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- Poj 2488 A Knight's Journey(搜索)
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ 2488 A Knight's Journey【DFS】
补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- POJ 2488 A Knight's Journey
题意:给一个n×m的棋盘,如果一个骑士可以从任意一个位置出发不重复的走遍棋盘的每个格子就输出字典序最短的路径. 解法:dfs.暴搜n×m次,只是被字典序输出坑了……而且字母是列序号数字是行序号……这两 ...
随机推荐
- eclipse 4.3 汉化
打开浏览器,浏览“参考资料”内给出的“eclipse语言包下载”地址,在博客新页面找到地址链接,如图所示.“Babel Language...”开头的一栏下面就是各个eclise版本的语言包,此处以I ...
- gridgroup行内编辑删除
Ext.define('Task', { extend: 'Ext.data.Model', idProperty: 'taskId', fields: [ { name: 'projectId', ...
- python使用电子邮件模块smtplib的方法(发送图片 附件)实用可行
Smptp类定义:smtplib.SMTP(host[,port[,local_hostname[,,timeout]]]),作为SMTP的构造函数,功能是与smtp服务器建立连接,在连接成功后,就可 ...
- LeetCode Problem 2:Two Sum
描述: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- 【BZOJ2213】[Poi2011]Difference DP
[BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...
- 【BZOJ4199】[Noi2015]品酒大会 后缀数组+并查集
[BZOJ4199][Noi2015]品酒大会 题面:http://www.lydsy.com/JudgeOnline/wttl/thread.php?tid=2144 题解:听说能用SAM?SA默默 ...
- C# 调用ArcGIS server admin api
一.AGS server admin api 介绍 1.1什么是admin api AGS Server Admin api 官方的称呼是 AGS Server administrator api, ...
- POCO c++ 使用例子
.定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using P ...
- 0x08 MySQL 超详细-索引原理&慢查询优化【转-多图】(重点)
Content From——Egon's Blog http://www.cnblogs.com/linhaifeng/articles/7274563.html#top 0x01 介绍 为何要有索引 ...
- Python Redis pipeline操作(秒杀实现)
设想这样的一个场景,你要批量的执行一系列redis命令,例如执行100次get key,这时你要向redis请求100次+获取响应100次.如果能一次性将100个请求提交给redis server,执 ...