poj2488 A Knight's Journey
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 24840 | Accepted: 8412 |
Description

Background
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.
Input
Output
If no such path exist, you should output impossible on a single line.
Sample Input
3
1 1
2 3
4 3
Sample Output
Scenario #1:
A1 Scenario #2:
impossible Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
Source
简单的深搜,不多说,直接上代码!
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int pathlow[30],pathdown[30],visit[30][30];
int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}},n,m;//这里注意是字典序最小
bool dfs(int low,int down,int num)
{
int i,x,y;
if(num==n*m)
{ for(i=0;i<n*m;i++)
{ printf("%c%d",'A'+pathdown[i],pathlow[i]+1);
}
return true;
} for(i=0;i<8;i++)
{
x=low+dir[i][0];
y=down+dir[i][1];
pathlow[num]=x;
pathdown[num]=y;
if(x>=0&&x<n&&y>=0&&y<m&&(!visit[x][y]))
{ visit[x][y]=1;
if(dfs(x,y,num+1))
{ return true;
}
else
{ visit[x][y]=0;//这里要注意,一定要重新标记为0
}
} }
return false; }
int main ()
{
int t,i;
while(scanf("%d",&t)!=EOF)
{ for(i=1;i<=t;i++)
{
printf("Scenario #%d:\n",i);
scanf("%d%d",&n,&m);
memset(visit,0,sizeof(visit));
visit[0][0]=1;
pathlow[0]=0;
pathdown[0]=0;
if(!dfs(0,0,1))
{ printf("impossible");
}
printf("\n\n"); }
} return 0;
}
poj2488 A Knight's Journey的更多相关文章
- 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 ...
- POJ2488A Knight's Journey
http://poj.org/problem?id=2488 题意 : 给你棋盘大小,判断马能否走完棋盘上所有格子,前提是不走已经走过的格子,然后输出时按照字典序排序的第一种路径 思路 : 这个题吧, ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
- 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) ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
随机推荐
- 喜欢的女生快被别人抢走了,我敢怎么抢? - V2EX
喜欢的女生快被别人抢走了,我敢怎么抢? - V2EX 三大定律镇楼: 第一定律:永远不要以为女生对你有好感.第二定律:告白等于见光死.第三定律:秀恩爱分得快.
- A Simple Problem with Integers(100棵树状数组)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Grails重定向以及Render、Session的使用
近期发现网上grails因为用的人不算非常多,相关的资料也不是那么easy找到,随着学习的深入打算把我的学习心得定期分享出来.也给学习grails的同胞一个偷懒的机会,我的原则是以最简洁的方式学会Gr ...
- mysql binlog参数设置
1.mysql有许多系统变量,可以设置,系统变量设置不同,不同的系统将导致执行状态. 故mysql提供两组命令,分别查看系统设置和执行状态. 1.系统设置: SHOW [GLOBAL | SESSIO ...
- 【数位DP】【HDU2089】不要62
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- .NET MySQL的参数化查询
MySqlConnection conn = new MySqlConnection(SqlConnnectString); MySqlCommand cmd = new MySqlCommand(& ...
- 分享一个自己写的基于JQuery的一个Web背景切换的Demo
这个效果主要有两个特点: 1. 背景切换的渐变 2. 背景大小自适应 3. 背景自适应保持比例同时, 相对居中 js源码: (function ($) { $.fn.bgChange = functi ...
- jQuery中$.getJSON的返回值问题
在使用$.getJSON获得数据库的返回值后,想将该值return传给其他函数.结果遇到问题. $.getJSON(url, data, function(result) { return resul ...
- 二维数组在text,image的应用
NSArray *imageArr = @[@[@"查看地图",@"map_hy.png"], @[@"联系号码",@"phone ...
- ProxyCreationEnabled 和 LazyLoadingEnabled 的关系
我们会使用这两个设置来决定是否加载导航属性.默认情况这两个值都是true的,也就是说会以延迟加载的方式加载导航属性,也就是当我们访问导航属性的时候才会去查数据库获取导航属性的数据.db.Configu ...