您的位置:首页 > 其它

poj_2243/uva439 Knight Moves(bfs經典)

2014-07-17 00:35 381 查看
 

 

Knight Moves

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11233 Accepted: 6337
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 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
 
題目大意:給你兩個棋盤的位置,分別為起點和終點,求最少幾步能從起點到終點

騎士遍歷問題,對西洋棋不熟,開始不知道騎士的走法和棋盤結構,查了之後才知道騎士走法也是像中國象棋中的馬那樣,走“日”字,,附圖,轉載別人的(如有侵權,請告訴我)



 

這題我在Uva和poj上都AC了,在poj上WA,然後改了bfs()函數就過了。而且單獨在poj上提交會出現訪問禁止,很奇怪。下面是在virtual judge上用poj來judge的AC的

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int dirx[]={-1,-1,-2,-2,1,1,2,2},diry[]={-2,2,-1,1,-2,2,-1,1};
int visit[10][10];
char s[3],e[3];
struct knight{
int x,y,d;
}k;
void bfs(int s1,int s2,int e1,int e2)
{
knight k1,h;
int i;
queue<knight>q;
if(s1==e1 && s2==e2) {printf("To get from %s to %s takes %d knight moves.\n",s,e,0); return;}
while(!q.empty()) q.pop();
k.x=s1;
k.y=s2;
k.d=0;
q.push(k);
visit[s1][s2]=1;
while(!q.empty())
{
k=q.front();
q.pop();
//      printf("x=%d y=%d\n",k.x,k.y);
for(i=0;i<8;i++)
{
int u=k.x+dirx[i];
int v=k.y+diry[i];
if(u>0 && u<=8 && v>0 && v<=8 && !visit[u][v])
{
k1.x=u; k1.y=v;
visit[u][v]=1;
k1.d=k.d+1;
q.push(k1);
if(u==e1 && v==e2)  {printf("To get from %s to %s takes %d knight moves.\n",s,e,k1.d); return;}
}
}
}
//  return -1;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%s%s",s,e)!=EOF)
{
int s1=s[0]-'a'+1;
int s2=s[1]-'0';
int e1=e[0]-'a'+1;
int e2=e[1]-'0';
memset(visit,0,sizeof(visit));
int ans;
//      ans=bfs(s1,s2,e1,e2);          //好像WA的問題出在這裡!!!
//      printf("To get from %s to %s takes %d knight moves.\n",s,e,ans);
bfs(s1,s2,e1,e2);                   //這是沒有返回值的函數,答案直接在函數中輸出
memset(s,'\0',sizeof(s));
memset(e,'\0',sizeof(e));
}
return 0;
}

 

下面是WA的代碼(若有大神看出問題,求指教!):

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int dirx[]={-1,-1,-2,-2,1,1,2,2},diry[]={-2,2,-1,1,-2,2,-1,1};
int visit[10][10];
char s[3],e[3];
struct knight{
int x,y,d;
}k;
int bfs(int s1,int s2,int e1,int e2)
{
knight k1,h;
int i;
queue<knight>q;
if(s1==e1 && s2==e2) return 0;
while(!q.empty()) q.pop();
k.x=s1;
k.y=s2;
k.d=0;
q.push(k);
visit[s1][s2]=1;
while(!q.empty())
{
k=q.front();
q.pop();
//      printf("x=%d y=%d\n",k.x,k.y);
for(i=0;i<8;i++)
{
int u=k.x+dirx[i];
int v=k.y+diry[i];
if(u>0 && u<=8 && v>0 && v<=8 && !visit[u][v])
{
k1.x=u; k1.y=v;
visit[u][v]=1;
k1.d=k.d+1;
q.push(k1);
if(u==e1 && v==e2) return k1.d;
}
}
}
return -1;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%s%s",s,e)!=EOF)
{
int s1=s[0]-'a'+1;
int s2=s[1]-'0';
int e1=e[0]-'a'+1;
int e2=e[1]-'0';
//      printf("%d%d  %d%d\n",s1,s2,e1,e2);
memset(visit,0,sizeof(visit));
int ans;
ans=bfs(s1,s2,e1,e2);  //把上面的函數改成有返回值的就會WA!!!不知道問題出在哪!!!
//      if(ans>=0)
printf("To get from %s to %s takes %d knight moves.\n",s,e,ans);
memset(s,'\0',sizeof(s));
memset(e,'\0',sizeof(e));
}
return 0;
}


 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: