hdu 1372 Knight Moves BFS解法 + A*算法 两种解法AC

来源:互联网 发布:java urlencoder utf8 编辑:程序博客网 时间:2024/05/16 09:10

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 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.
 
水题一枚,值得注意的国际象棋中马的走法和 象棋中是一样的,走日。
一开始用DFS,TLE,就改成了BFS。另外坐标全是小于10的数,所以我另pos = x*10+y;这样便于操作也便于分离。
看代码:
#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <queue>using namespace std ;int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};queue<int> 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 ;}

/**************************************update2015/2/14**********************************************************/

上面的BFS 花费了200MS+,而A*只用30MS+,孰优孰劣,一看就明白了。

A*算法代码:

#include <cstdio>#include <queue>#include <cstring>#include <cstdlib>#define MAX 10using namespace std ;struct Knode{int x,y,g,h,f,step;bool operator<(const Knode &k) const{return f > k.f ;} };int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};bool close[MAX][MAX] ;int row = 8 , col = 8 ,desX,desY,ans;priority_queue<Knode> p ;bool judge(Knode t){if(t.x<0||t.x>=row || t.y<0||t.y>=col){return false ;}return true ;}int Heuristic(const Knode &a)//manhattan估价函数{                    return (abs(a.x-desX)+abs(a.y-desY))*10;}void AStar(){while(!p.empty()){Knode k = p.top();p.pop();close[k.x][k.y] = true ;if(close[desX][desY]){ans = k.step ;break ;}for(int i = 0 ; i < 8 ; ++i){Knode t ;t.x = k.x+dir[i][0] , t.y = k.y+dir[i][1] ;if(!judge(t)){continue ;}if(!close[t.x][t.y]){t.g = k.g + 23 ;//23表示根号5乘以10再取其ceilt.h = Heuristic(t) ;t.f = t.g+t.h ;t.step = k.step+1 ;p.push(t) ; }}}}int main(){char str[6];while(gets(str)){Knode k ;k.x = (str[0]-'a') , k.y = (str[1]-'1') ;k.f = k.g = k.h = k.step = 0 ;p.push(k) ;desX = str[3]-'a' , desY = str[4]-'1' ;ans = 0 ;memset(close,false,sizeof(close)) ;AStar();printf("To get from %c%c to %c%c takes %d knight moves.\n",str[0],str[1],str[3],str[4],ans) ;while(!p.empty())p.pop() ;}return 0 ;}

PS:我这代码在关闭控制台的时候就会停止工作。。不知道为啥,但是可以正常AC。。有大神看出来BUG,请告知我一声,谢谢。

/*******************************************************************************************************************/

我还是想把我超时的DFS代码贴上来,虽然超时,但是思路还是对的。
Ps:如果谁有剪枝的思路也可以和我交流一下,

//超时代码 #include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#define MAX 90using 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 ;}

与大家共勉。


0 0
原创粉丝点击