439 - Knight Moves
来源:互联网 发布:sd卡格式化后数据恢复 编辑:程序博客网 时间:2024/05/16 11:55
A friend of you isdoing research on the Traveling Knight Problem (TKP) where youare to find the shortest closed tour of knight moves that visits each square ofa given set of n squares on a chessboard exactly once. Hethinks that the most difficult part of the problem is determining the smallestnumber of knight moves between two given squares and that, once you haveaccomplished this, finding the tour would be easy.
Of course you knowthat it is vice versa. So you offer him to write a program that solves the"difficult" part.
Your job is towrite a program that takes two squares a and b asinput and then determines the number of knight moves on a shortest routefrom a to b.
Input Specification
The input filewill contain one or more test cases. Each test case consists of one linecontaining two squares separated by one space. A square is a string consistingof a letter (a-h) representing the column and a digit (1-8) representing therow on the chessboard.
Output Specification
For each testcase, print one line saying "To get from xx to yy takes n knightmoves.".
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To getfrom e2 to e4 takes 2 knight moves.
To get from a1 tob2 takes 4 knight moves.
To get from b2 toc3 takes 2 knight moves.
To get from a1 toh8 takes 6 knight moves.
To get from a1 toh7 takes 5 knight moves.
To get from h8 toa1 takes 6 knight moves.
To get from b1 toc3 takes 1 knight moves.
To get from f6 tof6 takes 0 knight moves.
代码:
#include<iostream>
#include<cstring>
#include<queue>
using namespacestd;
struct dot
{
int r,c;
dot(int r=0,int c=0):r(r),c(c) {}
};
int vis[8][8];
int d[8][8];
int dr[] ={-2,-2,-1,-1, 1,1, 2,2};
int dc[] = {-1,1,-2, 2,-2,2,-1,1};
int bfs(int r1,intc1,int r2,int c2);
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
memset(vis,0,sizeof(vis));
intans=bfs(s1[0]-'a',s1[1]-'1',s2[0]-'a',s2[1]-'1');
cout<<"To get from"<<s1<<" to "<<s2<<" takes"<<ans<<" knight moves.\n";
}
return 0;
}
int bfs(int r1,intc1,int r2,int c2)
{
if(r1==r2&&c1==c2)
{
return 0;
}
queue<dot> q;
q.push(dot(r1,c1));
vis[r1][c1]=1;
d[r1][c1]=0;
while(!q.empty())
{
dot a=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int newr=a.r+dr[i];
int newc=a.c+dc[i];
if(newr>=0&&newr<8&&newc>=0&&newc<8&&vis[newr][newc]==0)
{
q.push(dot(newr,newc));
d[newr][newc]=d[a.r][a.c]+1;
vis[newr][newc]=1;
if(newr==r2&&newc==c2)
{
return d[newr][newc];
}
}
}
}
return -1;
}
- uva 439 knight moves
- uva 439 - Knight Moves
- 439 - Knight Moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- UVaOJ 439 - Knight Moves
- uva 439 - Knight Moves
- uva-439 - Knight Moves
- UVa 439 - Knight Moves
- UVa 439 - Knight Moves
- 439 - Knight Moves
- UVA 439 - Knight Moves
- 439 - Knight Moves//bfs
- uva 439 Knight Moves
- UVa 439 - Knight Moves
- uva 439 - Knight Moves
- UVa:439 Knight Moves
- uva 439 - Knight Moves
- 更换主域服务器,如何完整迁移AD和DNS
- pat1062 Talent and Virtue
- 处理RCV_TRANSACTIONS_INTERFACE的数据
- 交叉 valgrind
- Connection.setAutoCommit使用的注意事项
- 439 - Knight Moves
- maven install 跳过 测试 test
- 536 - Tree Recovery
- swift 字符串
- Android 中如何才能让 StackView 的 OnItemSelectedListener 侦听器响应选中事件
- 根据时间生成订单
- hdu 2149 Public Sale
- 673 - Parentheses Balance
- maven 生成可执行jar