hdu 1372 Knight Moves BFS解法
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7494 Accepted Submission(s): 4486
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.
水題一枚,值得注意的國際象棋中馬的走法和 象棋中是一樣的,走日。
一開始用DFS,TLE,就改成了BFS。另外坐標全是小於10的數,所以我另pos = x*10+y;這樣便於操作也便於分離。
看代碼:
#include
#include
#include
#include
#include
using namespace std ;
int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
queue q;
int visited[15][15] ;
int des , ans = INT_MAX;
bool judge(int x ,int y)
{
if(x<=0||x>8 || y<=0||y>8)
{
return false;
}
return true ;
}
void BFS(int pos)
{
q.push(pos) ;
while(!q.empty())
{
int now = q.front();
int x=now/10,y=now%10 ;
q.pop();
if(now == des)
{
if(visited[x][y] < ans)
{
ans = visited[x][y] ;
}
continue ;
}
else if(visited[x][y]>=ans)
{
continue ;
}
for(int i = 0 ; i < 8 ; ++i)
{
int nextX = x+dir[i][0] , nextY = y+dir[i][1] ;
if(judge(nextX,nextY) && (visited[nextX][nextY] == 0 || visited[nextX][nextY]<=visited[x][y]+1))
{
visited[nextX][nextY] = visited[x][y]+1 ;
q.push(nextX*10+nextY) ;
}
}
}
}
int main()
{
char p1[4],p2[4];
while(scanf("%s%s",p1,p2)!=EOF)
{
ans = INT_MAX ;
des = (p2[0]-'a'+1)*10+(p2[1]-'0');
memset(visited,0,sizeof(visited)) ;
BFS((p1[0]-'a'+1)*10+(p1[1]-'0')) ;
printf("To get from %s to %s takes %d knight moves.\n",p1,p2,ans);
}
return 0 ;
}
我還是想把我超時的DFS代碼貼上來,雖然超時,但是思路還是對的。
Ps:如果誰有剪枝的思路也可以和我交流一下,
//超時代碼
#include
#include
#include
#include
#define MAX 90
using namespace std ;
int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
bool visited[MAX] ;
int des , ans = INT_MAX;
bool judge(int x ,int y)
{
if(x<=0||x>8 || y<=0||y>8)
{
return false;
}
if(visited[x*10+y])
{
return false;
}
return true ;
}
void DFS(int pos , int step)
{
if(pos == des)
{
if(ans>step)
{
ans = step ;
}
return ;
}
else if(step>=ans)
{
return ;
}
else
{
int x = pos/10,y = pos%10 ;
for(int i = 0 ; i < 8 ; ++i)
{
int nextX = x + dir[i][0],nextY = y + dir[i][1] ;
if(judge(nextX,nextY))
{
visited[nextX*10+nextY] = true ;
DFS(nextX*10+nextY,step+1) ;
visited[nextX*10+nextY] = false ;
}
}
}
}
int main()
{
char p1[4],p2[4];
while(scanf("%s%s",p1,p2)!=EOF)
{
ans = INT_MAX ;
des = (p2[0]-'a'+1)*10+(p2[1]-'0');
sizeof(visited,0,sizeof(visited)) ;
DFS((p1[0]-'a'+1)*10+(p1[1]-'0'),0) ;
printf("%d\n",ans);
}
return 0 ;
}
與大家共勉。