首页 > 代码库 > poj2243
poj2243
题目链接:
http://poj.org/problem?id=2243
题目:
Knight Moves
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10932 | Accepted: 6171 |
Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves. To get from a1 to b2 takes 4 knight moves. To get from b2 to c3 takes 2 knight moves. To get from a1 to h8 takes 6 knight moves. To get from a1 to h7 takes 5 knight moves. To get from h8 to a1 takes 6 knight moves. To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves.
Source
Ulm Local 1996
这道题目是搜索的入门提。。我用两种方法写了一下。。。
bfs版本:
#include<cstdio> #include<queue> #include <vector> #include<iostream> #include<cstring> #include<algorithm> const int maxn=10; using namespace std; bool vis[maxn][maxn]; int move[8][2]={{-1,2},{1,2},{-1,-2},{1,-2},{-2,1},{2,1},{-2,-1},{2,-1}}; int step[maxn][maxn]; struct Map { int x,y; }; int check(int x,int y) { if(x>=1&&y>=1&&y<=8&&x<=8) return 1; return 0; } int bfs(int sx,int sy,int ex,int ey) { queue<Map>q; Map a; a.x=sx; a.y=sy; q.push(a); vis[sx][sy]=true; while(!q.empty()) { Map temp=q.front(); q.pop(); for(int i=0;i<8;i++) { int tx=temp.x+move[i][0]; int ty=temp.y+move[i][1]; if(check(tx,ty)&&!vis[tx][ty]) { step[tx][ty]=step[temp.x][temp.y]+1; vis[tx][ty]=true; if(tx==ex&&ty==ey) return step[ex][ey]; Map tmp; tmp.x=tx; tmp.y=ty; q.push(tmp); } } } } int main() { int sx,sy,ex,ey; char s,s1,c; while(scanf("%c%d%c%c%d",&s,&sy,&c,&s1,&ey)!=EOF) { memset(vis,false,sizeof(vis)); memset(step,0,sizeof(step)); sx=s-'a'+1; ex=s1-'a'+1; step[sx][sy]=0; if(sx==ex&&sy==ey) printf("To get from %c%d to %c%d takes %d knight moves.\n",s,sy,s1,ey,0); else printf("To get from %c%d to %c%d takes %d knight moves.\n",s,sy,s1,ey,bfs(sx,sy,ex,ey)); getchar(); } return 0; }
Dfs版本的:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cstdlib> #define INF 0x3f3f3f3f const int maxn=10; using namespace std; bool vis[maxn][maxn]; int step[maxn][maxn]; int ans,ex,ey; int dx[8]={-1,-1,1,1,2,2,-2,-2}; int dy[8]={2,-2,2,-2,1,-1,1,-1}; void dfs(int sx,int sy,int depth) { if(sx<1||sy<1||sx>8||sy>8||depth>=ans) return; if(sx==ex&&sy==ey) { ans=min(ans,depth); return; } if(!vis[sx][sy]) { vis[sx][sy]=true; step[sx][sy]=depth; } else { if(step[sx][sy]<=depth) return; step[sx][sy]=depth; } for(int i=0;i<8;i++) { int tx=sx+dx[i]; int ty=sy+dy[i]; dfs(tx,ty,depth+1); } } int main() { char s,s1,c; int sx,sy; while(scanf("%c%d%c%c%d",&s,&sy,&c,&s1,&ey)!=EOF) { memset(vis,false,sizeof(vis)); memset(step,INF,sizeof(step)); sx=s-'a'+1; ex=s1-'a'+1; ans=10000; dfs(sx,sy,0); printf("To get from %c%d to %c%d takes %d knight moves.\n",s,sy,s1,ey,ans); getchar(); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。