【hdu 1372】 Knight Moves (BFS)

Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 20   Accepted Submission(s) : 10
Problem 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.
 

Input
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
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
University of Ulm Local Contest 1996
 

//進隊後visit數組標記爲true;map數組記錄走的步數,head指向隊頭,tail指向隊尾。
//題目中的column(列)剛好對應到x,row(行)對應到y
#include<cstdio>
#include<cstring>
bool visit[10][10];
int map[10][10];
struct node
{
       int x,y;
};
int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
node que[100];
int BFS(node s,node t)
{
	 if(s.x==t.x&&s.y==t.y) return 0;
     int head=0,tail=0;
     que[tail++]=s;
     visit[s.x][s.y]=true;
     node tt;
     while(head<tail)
     {
          tt=que[head++];
          for(int i=0;i<8;i++)
          {
               node ss;
               ss.x=tt.x+dir[i][0];
               ss.y=tt.y+dir[i][1];
			   cnt++;
               if(ss.x>=1&&ss.x<=8&&ss.y>=1&&ss.y<=8&&!visit[ss.x][ss.y])
               {
				   map[ss.x][ss.y]=map[tt.x][tt.y]+1;
                  if(ss.x==t.x&&ss.y==t.y) return map[ss.x][ss.y];
                  que[tail++]=ss;
                  visit[ss.x][ss.y]=true;
               }    
          }      
     }
}
int main()
{
    char c1,c2,c;
    node st,ed;
    while(scanf("%c%d%c%c%d",&c1,&st.y,&c,&c2,&ed.y)!=EOF)//8row 8column
    {
		getchar();
        memset(visit,false,sizeof(visit));
		memset(map,0,sizeof(map));
        st.x=c1-'a'+1;
        ed.x=c2-'a'+1; 
        cnt=0; 
        printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,st.y,c2,ed.y,BFS(st,ed));
        
    }
    return 0;
}
    




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章