Knight Moves
2016-04-06 15:13
375 查看
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. 题意:给你两个坐标求棋子马过去的最少步数; 解题思路:最短路径问题,有新意的地方就是不再是上下左右走,而是跳马,八个方向用广搜,第一个搜到的就是最短路径; 感悟:一道中规中矩的题; 代码:
#include
#include
#include
#include
#define maxn 10
using namespace std;
int dir[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}};
int visit[maxn][maxn];
int check(int a,int b)
{
if(a<1||a>8||b<1||b>8||visit[a][b])
return 1;
else
return 0;
}
struct node
{
int x,y;
int times;
}start,fr,a;
int bfs(int sx,int sy,int x,int y)
{
memset(visit,0,sizeof(visit));
queue Q;
start.x=sx;
start.y=sy;
start.times=0;
visit[start.x][start.y]=1;
Q.push(start);
while(!Q.empty())
{
fr=Q.front();
Q.pop();
//cout<<"x="<<x<<" "<<"y="<<y<<endl;
if(fr.x==x&&fr.y==y)
return fr.times;
for(int i=0;i<8;i++)
{
a.x=fr.x+dir[i][0];
a.y=fr.y+dir[i][1];
if(check(a.x,a.y))
continue;
a.times=fr.times+1;
visit[a.x][a.y]=1;
Q.push(a);
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
char a,b;
int sx,sy,x,y;
while(~scanf("%c%d %c%d\n",&a,&sy,&b,&y))
{
//cout<<"sx="<<a<<endl;
//cout<<"sy="<<sy<<endl;
//cout<<"x="<<b<<endl;
//cout<<"y="<<y<<endl;
sx=a-'a'+1;
x=b-'a'+1;
//坐标转化成数字
int ans=bfs(sx,sy,x,y);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,sy,b,y,ans);
}
return 0;
}
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. 题意:给你两个坐标求棋子马过去的最少步数; 解题思路:最短路径问题,有新意的地方就是不再是上下左右走,而是跳马,八个方向用广搜,第一个搜到的就是最短路径; 感悟:一道中规中矩的题; 代码:
#include
#include
#include
#include
#define maxn 10
using namespace std;
int dir[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}};
int visit[maxn][maxn];
int check(int a,int b)
{
if(a<1||a>8||b<1||b>8||visit[a][b])
return 1;
else
return 0;
}
struct node
{
int x,y;
int times;
}start,fr,a;
int bfs(int sx,int sy,int x,int y)
{
memset(visit,0,sizeof(visit));
queue Q;
start.x=sx;
start.y=sy;
start.times=0;
visit[start.x][start.y]=1;
Q.push(start);
while(!Q.empty())
{
fr=Q.front();
Q.pop();
//cout<<"x="<<x<<" "<<"y="<<y<<endl;
if(fr.x==x&&fr.y==y)
return fr.times;
for(int i=0;i<8;i++)
{
a.x=fr.x+dir[i][0];
a.y=fr.y+dir[i][1];
if(check(a.x,a.y))
continue;
a.times=fr.times+1;
visit[a.x][a.y]=1;
Q.push(a);
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
char a,b;
int sx,sy,x,y;
while(~scanf("%c%d %c%d\n",&a,&sy,&b,&y))
{
//cout<<"sx="<<a<<endl;
//cout<<"sy="<<sy<<endl;
//cout<<"x="<<b<<endl;
//cout<<"y="<<y<<endl;
sx=a-'a'+1;
x=b-'a'+1;
//坐标转化成数字
int ans=bfs(sx,sy,x,y);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,sy,b,y,ans);
}
return 0;
}
相关文章推荐
- Knight Moves
- V-Play入门手册1-如何在5分钟内把你游戏或应用程序带到你的移动设备上?
- XUtils===XUtils3框架的基本使用方法(二)
- mbps
- RtlPrefixUnicodeString routine
- list 转 set 排序问题
- 返回一个二维整数数组中最大联通子数组的和
- (LinkedList)Intersection of Two Linked Lists
- Hololens 开发环境配置
- Python运算符
- HDU1754 线段树 单点修改 最大值(基础题)
- iOS 定位
- java中的多线程包--- Executor详细介绍 打造基于Executor的Web服务器
- Design guidelines for application performance
- ipad-group table的背景色设置成透明的
- iOS-字符串转化成NSDate类型 计算与当前时间的相差 月数 天数
- Apache问题解决:httpd: Could not reliably determine the server's fully qualified domain name
- iOS 本地缓存 归档数据模型化
- Preorder, Inorder, and Postorder非递归总结
- 管理者,你的团队持续可用吗