Knight Moves bfs(广度优先搜索)
来源:互联网 发布:淘宝开店流程 手机 编辑:程序博客网 时间:2024/06/03 17:47
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11823 Accepted Submission(s): 6962
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.
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.
Source
University of Ulm Local Contest 1996
这题用dfs和bfs都可以;
题目的意思:从x,y到达ax,ay的最小步数,走的是马的步子,也就是走日子步;
这里用bfs加dp来写
用dp【i】【j】表示从x,y到i,j的最小步数;
usingnamespacestd;
int c[]={-1,1,2,-2,-1,1,2,-2};
int b[]={2,-2,-1,1,-2,2,1,-1};
struct node{
int x;
int y;
};
queue<node>q;
bool visit[200][200];//访问标志
int ax,ay;
int sum=0;
int dp[200][200];
char e[12],r[12];
voidbfs(int x,int y){
if(x==ax&&ay==y)
{
cout<<"To get from "<<e<<" to "<<r<<" takes 0 knight moves."<<endl;
return ;
}
node a;
a.x=x;a.y=y;
q.push(a);
while(!q.empty())
{
sum++;
x=q.front().x;
y=q.front().y;
visit[x][y]=true;
q.pop();
int i;
for(i=0;i<8;i++)
{
int nx=x+c[i];
int ny=y+b[i];
a.x=nx;
a.y=ny;
if(a.x>=1&&a.x<=8&&a.y>=1&&a.y<=8&&!visit[nx][ny])
{
dp[nx][ny]=min(dp[x][y]+1,dp[nx][ny]);
q.push(a);
}
}
}
cout<<"To get from "<<e<<" to "<<r<<" takes ";
cout<<dp[ax][ay]<<" knight moves."<<endl;
}
intmain(){
int x,y;
while(cin>>e>>r)
{
memset(visit,false,sizeof(visit));
x=e[0]-'a'+1;
y=e[1]-'0';
ax=r[0]-'a'+1;
ay=r[1]-'0';
int i,j;
for(i=0;i<=20;i++)
{
for(j=0;j<=20;j++)
{
dp[i][j]=100;
}
}
dp[x][y]=0;
bfs(x,y);
}
return0;
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
阅读全文
1 0
- Knight Moves bfs(广度优先搜索)
- Knight Moves--广度优先搜索
- HDU 1372 广度优先搜索(BFS) Knight Moves
- HDU 1372 Knight Moves 广度优先搜索 bfs
- POJ1915 - Knight Moves - 广度优先搜索
- C++广度优先搜索之Knight Moves
- POJ 2243 ZOJ 1091 UVaOJ 439 Knight moves(BFS广度优先搜索)
- Knight Moves(双向广度搜索)
- pku 1915 Knight Moves(双向广度优先搜索)
- HDU/HDOJ 1372 Knight Moves(骑士游走问题) 简单广度优先搜索
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- BFS(广度优先搜索)
- Knight Moves(bfs)
- Knight Moves(BFS)
- Knight Moves(BFS)
- 关于maven的心得
- BZOJ2705: [SDOI2012]Longge的问题(欧拉函数)
- MySQL DELETE 语句
- 排序算法
- 深入理解C语言指针
- Knight Moves bfs(广度优先搜索)
- PDF转换成图片JPG
- http传输协议
- 反省中
- 设计模式之观察者模式
- tesseract 识别中文字符
- hdu 2665 Kth number(主席树)
- 没有显示器情况下,安装和使用树莓派
- 安卓虚拟机启动后报错: 类似 SDK Manager] Error: Error parsing .....devices.xml 解决方案