zoj1091Knight Moves dfs+bfs+floyd

来源:互联网 发布:搜索网站排名优化策略 编辑:程序博客网 时间:2024/05/29 15:19
Knight Moves

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 nsquares 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.

Input Specification

The input file 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 Specification

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

Sample Input

e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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.
题意,跟下象棋一样,棋盘上有一个马,只会走 日 ,求问从起点走到终点最少需要多少步。
附深搜代码:

#include<stdio.h>//深搜 #include<string.h>#include<algorithm>int a[9][9],i,j,k,l,m,n,x,y,x1,y1,ans;int help[10][10];char c[100],c1[100];int deal(char c){int ans=c-'a'+1;return ans;}void dfs(int x,int y,int num){if(x<=0||y<=0||x>=9||y>=9||num>=help[x][y])return ;help[x][y]=num;dfs(x+1,y+2,num+1);dfs(x-1,y+2,num+1);dfs(x+1,y-2,num+1);dfs(x-1,y-2,num+1);dfs(x+2,y+1,num+1);dfs(x-2,y+1,num+1);dfs(x+2,y-1,num+1);dfs(x-2,y-1,num+1);}int main(){while(scanf("%s%s",c,c1)!=EOF){getchar();memset(help,100,sizeof(help));y=deal(c[0]);y1=deal(c1[0]);dfs(y,c[1]-'0',0);ans=help[y1][c1[1]-'0'];printf("To get from %s to %s takes %d knight moves.\n",c,c1,ans);}return 0;}

深搜还是比较简单的,代码也不长,但是有点耗时。

附0ms代码:

打表弗洛伊德:

#include<stdio.h>//弗洛伊德 #include<string.h>#include<algorithm>#define N 0x3f3f3fusing namespace std;int ans[100][100],i,j,k,l,m,n,x,y;char c[1000],s[1000];int bs(int x){if(x>0)return x;return -x;}int main(){memset(ans,N,sizeof(ans));for(i=0;i<=63;i++)for(j=0;j<=63;j++){ans[j][j]=0;x=i/8-j/8;x=bs(x);y=i%8-j%8;y=bs(y);if(x==2&&y==1||x==1&&y==2)ans[i][j]=ans[j][i]=1;}for(i=0;i<64;i++)for(j=0;j<64;j++)for(k=0;k<64;k++)if(ans[j][k]>ans[j][i]+ans[k][i])ans[j][k]=ans[j][i]+ans[k][i];while(scanf("%s%s",c,s)!=EOF){x=(c[0]-'a')*8+c[1]-'1';y=(s[0]-'a')*8+s[1]-'1';printf("To get from %s to %s takes %d knight moves.\n",c,s,ans[x][y]);}}

也不是很难;

附广搜:

#include<stdio.h>#include<queue>#include<stack>#include<string.h>#include<algorithm>using namespace std;struct node { int x; int y; int road; }from,to; char c[100],s[100]; int dx[]={1,1,2,2,-1,-1,-2,-2}; int dy[]={2,-2,1,-1,2,-2,1,-1}; int i,j,k,l,m,n; void bfs(char c[],char s[]) { queue<node> q; from.x=c[0]-'a'; from.y=c[1]-'1'; from.road=0; to.x=s[0]-'a'; to.y=s[1]-'1'; q.push(from);node team;while(true){from=q.front();q.pop() ;if(from.x==to.x&&from.y==to.y)break;for(i=0;i<8;i++){team.x=from.x+dx[i];team.y=from.y+dy[i];m=team.road=from.road+1;if(team.x<0||team.x>7||team.y<0||team.y>7)continue;q.push(team);}}printf("To get from %s to %s takes %d knight moves.\n",c,s,from.road );  } int main() { while(scanf("%s%s",c,s)!=EOF) { bfs(c,s); } }


1 0
原创粉丝点击