A Knight's Journey--DFS
来源:互联网 发布:淘宝店铺信用卡套现 编辑:程序博客网 时间:2024/05/29 11:31
A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 40883 Accepted: 13906
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
题目链接:http://poj.org/problem?id=2488
题目大意:
这个题的意思是说输入两个数代表棋盘的大小,然后骑士固定从左上角开始走,问能否使骑士走过整张棋盘,如果可以,输出路径,注意,按字典序遍历。
按照此顺序遍历。
我必须吐槽一下这个题,我和柏皓找bug找了整整一个上午,就是一个很水的dfs,但是,最后找到的bug结果是,我用结构体存的路径,他用的字符数组,然后就。。。我真服气了。在此放两个代码,一个是我的最开始的版本,一个是2.0版本。一样的思路。
代码:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int h;int vis[200][200];int x,y;struct node{ int a; int b;}xin[10000];int top;int kkk;int flag;char s[128],a[128];void dfs(int sx,int sy,int step){ //printf("%d %d\n",step,h); if(step==h) { s[top]=0; strcpy(a,s); flag=1; } if(flag) { return ; } if(sx-1>=1&&sy-2>=1&&!vis[sx-1][sy-2])//字典序8个方向 { s[top++]=sy-2+'A'-1; s[top++]=sx-1+'0'; vis[sx-1][sy-2]=1; dfs(sx-1,sy-2,step+1); vis[sx-1][sy-2]=0; top-=2; } if(sx+1<=x&&sy-2>=1&&!vis[sx+1][sy-2])//一样的套路 { s[top++]=sy-2+'A'-1; s[top++]=sx+1+'0'; vis[sx+1][sy-2]=1; dfs(sx+1,sy-2,step+1); vis[sx+1][sy-2]=0; top-=2; } if(sx-2>=1&&sy-1>=1&&!vis[sx-2][sy-1]) { s[top++]=sy-1+'A'-1; s[top++]=sx-2+'0'; vis[sx-2][sy-1]=1; dfs(sx-2,sy-1,step+1); vis[sx-2][sy-1]=0; top-=2; } if(sx+2<=x&&sy-1>=1&&!vis[sx+2][sy-1]) { s[top++]=sy-1+'A'-1; s[top++]=sx+2+'0'; vis[sx+2][sy-1]=1; dfs(sx+2,sy-1,step+1); vis[sx+2][sy-1]=0; top-=2; } if(sx-2>=1&&sy+1<=y&&!vis[sx-2][sy+1]) { s[top++]=sy+1+'A'-1; s[top++]=sx-2+'0'; vis[sx-2][sy+1]=1; dfs(sx-2,sy+1,step+1); vis[sx-2][sy+1]=0; top-=2; } if(sx+2<=x&&sy+1<=y&&!vis[sx+2][sy+1]) { s[top++]=sy+1+'A'-1; s[top++]=sx+2+'0'; vis[sx+2][sy+1]=1; dfs(sx+2,sy+1,step+1); vis[sx+2][sy+1]=0; top-=2; } if(sx-1>=1&&sy+2<=y&&!vis[sx-1][sy+2]) { s[top++]=sy+2+'A'-1; s[top++]=sx-1+'0'; vis[sx-1][sy+2]=1; dfs(sx-1,sy+2,step+1); vis[sx-1][sy+2]=0; top-=2; } if(sx+1<=x&&sy+2<=y&&!vis[sx+1][sy+2]) { s[top++]=sy+2+'A'-1; s[top++]=sx+1+'0'; vis[sx+1][sy+2]=1; dfs(sx+1,sy+2,step+1); vis[sx+1][sy+2]=0; top-=2; }}int main(){ int t; scanf("%d",&t); int kk=0; while(t--) { flag=0; kk++; memset(vis,0,sizeof(vis)); top=2; scanf("%d%d",&x,&y); h=x*y; s[0]='A',s[1]='1'; xin[0].a=1; xin[0].b=1; vis[1][1]=1; printf("Scenario #%d:\n",kk); dfs(1,1,1); if(flag) { printf("%s\n\n",a); } else printf("impossible\n\n"); } return 0;}
结构体太坑了,我到现在为止不知道为什么结构体会出错。
2.0版本代码
#include <cstdio>#include <cstring>using namespace std;struct node{ int a,b;} xin[30];char s[128],a[128];int top,kkk,flag,x,y,vis[30][30],dy[8]= {-2,-2,-1,-1,1,1,2,2},dx[8]= {-1,1,-2,2,-2,2,-1,1};//8个方向void dfs(int sx,int sy,int step){ if(step==x*y) { kkk=top; flag=1; } if(flag) return; int i,l,r; for (i=0; i<8; i++) { l=sx+dx[i]; r=sy+dy[i]; if (l>0&&l<=x&&r>0&&r<=y&&!vis[l][r]) { vis[l][r]=1; xin[top].a=r; xin[top++].b=l; dfs(l,r,step+1); top--; vis[l][r]=0; } }}int main(){ int t,kk=0; scanf("%d",&t); while(t--) { flag=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&x,&y); top=2; vis[1][1]=1; s[0]='A',s[1]='1'; printf("Scenario #%d:\n",++kk); dfs1(1,1,1); if(flag) printf("%s\n\n",a); else printf("impossible\n\n"); // printf("\n\n"); } return 0;}
注意此题输出时每一组多输出一行空格。
0 0
- A Knight's Journey(DFS)
- A Knight's Journey+dFS
- A Knight's Journey--DFS
- A Knight's Journey [dfs]
- DFS-A Knight's Journey
- [ACM]A Knight's Journey DFS应用
- pku 2488 A Knight's Journey DFS
- POJ 2488 A Knight's Journey DFS
- POJ 2488 A Knight's Journey (dfs)
- PKU2488 A Knight`s Journey--DFS
- POJ2488 A Knight's Journey(DFS)
- A Knight's Journey poj DFS
- POJ《A Knight's Journey》方法:DFS
- A Knight's Journey(简单dfs)
- POJ 2488 A Knight's Journey(DFS)
- POJ 2488 A Knight's Journey DFS
- POJ 2488 - A Knight's Journey (DFS)
- POJ 2488 A Knight's Journey【DFS】
- [01背包]vijos1625 精卫填海
- 如何在Ubuntu中打开PIXHAWK终端
- Android中RadioGroup和Fragment的应用
- HDU 3530 Subsequence(区间最值差>=m且<=k的最大长度、双单调队列)
- Retrofit2.0 公共参数(固定参数)
- A Knight's Journey--DFS
- CRT+容斥定理——Luck7 ( HDU 5768 ) ( 2016 Multi-University Training Contest 4 1005 )
- android studio 中 R.java 在哪
- 详细分析Linux动态库的使用方式
- Hbase迁移备份(后续更新)
- 常见编译错误信息
- Linux常用命令
- Vxworks系统学习----任务
- SDUT2107图的深度遍历