hdu 1372&&uva 439 KnightMoves(bfs)
2016-08-25 20:42
531 查看
Total Submission(s): 10356 Accepted Submission(s): 6094
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
[align=left]Sample Input[/align]
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
[align=left]Sample Output[/align]
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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
int num;
}a,b;
int d[8][2]={-2,1, -1,2, 1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1};
int maze[10][10];
int ax,ay,gx,gy;
char f[10],g[10];
int bfs(int x,int y)
{queue<node> q;
//node a,b;
a.x=x;a.y=y;a.num=0;
q.push(a);
while(q.size())
{
a=q.front();
q.pop();
if(a.x==gx&&a.y==gy)
return a.num;
for(int i=0;i<8;i++)
{
b.x=a.x+d[i][0];
b.y=a.y+d[i][1];
if(b.x>0&&b.x<9&&b.y>0&&b.y<9)
{
b.num=a.num+1;
if(b.x==gx&&b.y==gy)
return b.num;
q.push(b);
}
}
}
}
int main()
{
while(scanf("%s%s",f,g)!=EOF)
{
ax=f[0]-'a'+1;ay=f[1]-'0';
gx=g[0]-'a'+1;gy=g[1]-'0';
int t= bfs(ax,ay);
printf("To get from %s to %s takes %d knight moves.\n",f,g,t);
}
}
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10356 Accepted Submission(s): 6094
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
[align=left]Sample Input[/align]
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
[align=left]Sample Output[/align]
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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
int num;
}a,b;
int d[8][2]={-2,1, -1,2, 1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1};
int maze[10][10];
int ax,ay,gx,gy;
char f[10],g[10];
int bfs(int x,int y)
{queue<node> q;
//node a,b;
a.x=x;a.y=y;a.num=0;
q.push(a);
while(q.size())
{
a=q.front();
q.pop();
if(a.x==gx&&a.y==gy)
return a.num;
for(int i=0;i<8;i++)
{
b.x=a.x+d[i][0];
b.y=a.y+d[i][1];
if(b.x>0&&b.x<9&&b.y>0&&b.y<9)
{
b.num=a.num+1;
if(b.x==gx&&b.y==gy)
return b.num;
q.push(b);
}
}
}
}
int main()
{
while(scanf("%s%s",f,g)!=EOF)
{
ax=f[0]-'a'+1;ay=f[1]-'0';
gx=g[0]-'a'+1;gy=g[1]-'0';
int t= bfs(ax,ay);
printf("To get from %s to %s takes %d knight moves.\n",f,g,t);
}
}
相关文章推荐
- leetcode 387 First Unique Character in a String C++
- (四十四)、网络基础与TCP,UDP协议
- dct变换
- quality for code
- 个人主机配置为虚拟服务器
- 百度地图配置使用笔记(AndroidStudio)
- JavaScript学习笔记--语法
- HashMap封装查询到的数据
- 计算几何 BAPC 14 C itadel Construction (Gym 100526C )
- python第五天学习记录——模块
- java同一个类不同方法间的同步
- 作者(zhang854429783) eclipse通过tomcat热部署web项目
- 带你读开源—ASP.NET_MVC(二)
- BLE-NRF51822教程2-工程初始化流程
- iconv 文件编码转换
- BLE-NRF51822教程1-常用概念
- Item1 Understand template type deduction
- LOJ 1109 - False Ordering (排序)
- HttpServletRequest对象中的常用个的方法
- 医院门诊管理系统--华为OJ