题目:
Knight Moves
**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10658 Accepted Submission(s): 6264
**
\
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
\
Recommend
Eddy
\
Statistic | Submit | Discuss | Note
思路:
题意特别简单,给了一个国际象棋棋盘,给了起点和终点坐标,然后问最小步数,搜索时按照国际象棋马走“日”字来搜,刚开始用深搜超时了,一想用深搜肯定超时,因为深搜要搜完所有情况,所以要用广搜,顺便把深搜代码也附上。。
代码1(深搜DFS,未AC):
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int go[8][2]= {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
char a[10];
int vis[8][8];
int x1,y1,x2,y2,minn;
void dfs(int x,int y,int step)
{
vis[x][y]=1;
if(step>minn)return;
if(x==x2&&y==y2)
{
if(step<minn)minn=step;
return;
}
for(int i=0;i<8;i++)
{
int xx=x+go[i][0];
int yy=y+go[i][1];
if(xx>0&&xx<=8&&yy>0&&yy<=8&&vis[xx][yy]==0)
{
vis[xx][yy]=1;
dfs(xx,yy,step+1);
vis[xx][yy]=0;
}
}
}
int main()
{
while(gets(a))
{
mem(vis,0);
minn=9999;
if(a[0]=='a')x1=1;
if(a[0]=='b')x1=2;
if(a[0]=='c')x1=3;
if(a[0]=='d')x1=4;
if(a[0]=='e')x1=5;
if(a[0]=='f')x1=6;
if(a[0]=='g')x1=7;
if(a[0]=='h')x1=8;
y1=a[1]-'0';
if(a[3]=='a')x2=1;
if(a[3]=='b')x2=2;
if(a[3]=='c')x2=3;
if(a[3]=='d')x2=4;
if(a[3]=='e')x2=5;
if(a[3]=='f')x2=6;
if(a[3]=='g')x2=7;
if(a[3]=='h')x2=8;
y2=a[4]-'0';
if(x1==x2&&y1==y2)
printf("To get from %c%c to %c%c takes 0 knight moves.\n",a[0],a[1],a[3],a[4]);
else
{
dfs(x1,y1,0);
printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0],a[1],a[3],a[4],minn);
}
}
return 0;
}
代码2(广搜BFS,AC):
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int go[8][2]= {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
char a,b,c,d;
int vis[10][10];
int x1,y1,x2,y2,minn;
struct node
{
int x,y,s;
};
int bfs(int x,int y)
{
queue<node>q;
vis[x][y]=1;
node now,to;
now.x=x,now.y=y,now.s=0;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==x2&&now.y==y2)return now.s;
for(int i=0; i<8; i++)
{
int xx=now.x+go[i][0];
int yy=now.y+go[i][1];
if(xx>0&&xx<=8&&yy>0&&yy<=8&&vis[xx][yy]==0)
{
vis[xx][yy]=1;
to.x=xx,to.y=yy,to.s=now.s+1;
q.push(to);
}
}
}
return 0;
}
int main()
{
while(~scanf("%c%c %c%c",&a,&b,&c,&d))
{
mem(vis,0);
minn=9999;
if(a=='a')x1=1;
if(a=='b')x1=2;
if(a=='c')x1=3;
if(a=='d')x1=4;
if(a=='e')x1=5;
if(a=='f')x1=6;
if(a=='g')x1=7;
if(a=='h')x1=8;
y1=b-'0';
if(c=='a')x2=1;
if(c=='b')x2=2;
if(c=='c')x2=3;
if(c=='d')x2=4;
if(c=='e')x2=5;
if(c=='f')x2=6;
if(c=='g')x2=7;
if(c=='h')x2=8;
y2=d-'0';
if(x1==x2&&y1==y2)
printf("To get from %c%c to %c%c takes 0 knight moves.\n",a,b,c,d);
else
{
minn=bfs(x1,y1);
printf("To get from %c%c to %c%c takes %d knight moves.\n",a,b,c,d,minn);
}
getchar();
}
return 0;
}
\
\