poj 2488 DFS
来源:互联网 发布:农村淘宝宣传视频 编辑:程序博客网 时间:2024/06/05 11:25
A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 35974 Accepted: 12272
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.
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
The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.
If no such path exist, you should output impossible on a single line.
Sample Input
31 12 34 3
Sample Output
Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4
Source
TUD Programming Contest 2005, Darmstadt, Germany
题意是:看 马 是否可以在不重复走相同点的情况下,将所有的点都遍历一遍,并按字典序输出 .
A B C D E F
1
2
3
4
</pre><pre name="code" class="cpp">#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#define INF 0x3f3f3f3fusing namespace std;int n,m,num=0,flag=0;int hang[100];char lie[100];int vis[100][100];int dx[]= {-1,1,-2,2,-2,2,-1,1}; //注意顺序,为字典序输出int dy[]= {-2,-2,-1,-1,1,1,2,2};int Judge(int x,int y){ if(x<=n&&y<=m&&x>0&&y>0) return 1; return 0;}int DFS(int x,int y,int ans){ if(!Judge(x,y)) return 0; if(ans==n*m) { hang[ans]=x; lie[ans]=(char)(y-1+'A'); flag=1; return ans; } if(vis[x][y]==0&&!flag) //!flag 会保证第一次遍历成功后就不遍历了. { hang[ans]=x; lie[ans]=(char)(y-1+'A'); vis[x][y]=1; for(int i=0; i<8; i++) { int fx=dx[i]+x; int fy=dy[i]+y; if(Judge(fx,fy)&&vis[fx][fy]==0) { DFS(fx,fy,ans+1); } } vis[x][y]=0; //找不到还原 }}int main(){ int T; int Case=0,kk=1; while(~scanf("%d",&T)) { while(T--) { if(kk==1)kk=0;elseprintf("\n"); flag=0; num=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); DFS(1,1,1); Case++; printf("Scenario #%d:\n",Case); if(flag==0){ printf("impossible\n"); continue;} for(int i=1; i<=n*m; i++)printf("%c%d",lie[i],hang[i]); printf("\n"); } }}
1 0
- POJ 2488 _ DFS
- POJ 2488 DFS
- poj-2488-dfs
- DFS poj 2488
- POJ 2488(DFS)
- POJ 2488 DFS回溯
- POJ 2488 简单 DFS
- POJ 2488 DFS+回溯
- poj-2488-water-DFS
- poj 2488(DFS)
- poj 2488(dfs)
- POJ 2488 DFS
- poj 2488 dfs
- poj 2488 dfs
- poj 2488 DFS
- poj 2488 dfs+回溯
- POJ 2488(DFS)
- POJ 2488(DFS)
- 回顾:RPC远程过程调用协议
- 【Git分布式版本控制系统】Git安装与配置
- fsdff
- poj-2002
- leetcode:Basic Calculator II
- poj 2488 DFS
- CGContextAddArcToPoint和CGContextAddArc
- Console显示:Conversion to Dalvik format failed with error 1 ;your project contains errors) please fix
- Java里的按值传递与引用传递
- JDBC ORACLE 事物处理
- 关联表查询数据库
- Project configuration is not up-to-date with pom.xml. Run project configuration update
- SAP HANA会代替BW吗?
- C语言中输入输出格式控制