UVA 439 Knight Moves(BFS)
Knight Moves
Time Limit: 3000 MS
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 ofn 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 Specification
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 Specification
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<iostream>
#include<cstdio>
#include<string>
#include<queue> using namespace std; struct node{
int i,j,step;
node(int i0=0,int j0=0,int step0=0){i=i0,j=j0,step=step0;}
}mymap[10][10]; int dirX[8]={ 1,-1,-1, 1,2, 2,-2,-2};//
int dirY[8]={-2,-2, 2, 2,-1,1,-1,1},i1,j1,i2,j2;
char c1,c2; bool judge(int di,int dj){
if(di>=0&&di<8&&dj>=0&&dj<8) return true;
return false;
} bool read(){
if(cin>>c1>>j1>>c2>>j2){
i1=c1-'a',i2=c2-'a',j1--,j2--;
return true;
}
return false;
} void initial(){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
mymap[i][j].step=-1;
mymap[i][j].i=i;//哎。
mymap[i][j].j=j;//之前死在这。
}
}
} void bfs(){
queue <node> path;
bool ans =false;
mymap[i1][j1].step=0;
path.push(mymap[i1][j1]);
while(!path.empty()&&!ans){
node s=path.front();
path.pop();
for(int k=0;k<8;k++){
int di=s.i+dirY[k],dj=s.j+dirX[k];
if(judge(di,dj)&&mymap[di][dj].step==-1){
mymap[di][dj].step=s.step+1;
path.push(mymap[di][dj]);
}
if(di==i2&&dj==j2) {ans=true;break;}
}
}
} void outResult(){
printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,j1+1,c2,j2+1,mymap[i2][j2].step);
} int main(){
while(read()){
initial();
bfs();
outResult();
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
UVA 439 Knight Moves(BFS)的更多相关文章
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
- uva 439 Knight Moves 骑士移动
这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- hdu1372 Knight Moves BFS 搜索
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
随机推荐
- hdu2606(递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2606 题意: 用1*1,2*2,3*3,4*4的正方形填充4*n的矩形, 问有多少种不同填法. 分析 ...
- Laravel 中国 - 巨匠级PHP开发框架 Laravel 中国社区
http://m.baidu.com/from=844b/bd_page_type=1/ssid=0/uid=3151E6C0905477A13653132D762BB6FB/pu=sz%401320 ...
- Android 开源框架Universal-Image-Loader全然解析(二)--- 图片缓存策略具体解释
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...
- JAVA Socket(多个客户同时连接,信息共享) client (java/ruby)
第一步 充分理解Socket 1.什么是socket 所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字" ...
- Linux下SVN安装配置全程实录(转)
一.安装SVN默认安装到/usr/local/bin下面 二.创建仓库 svnadmin create /home/svnrepo /root/svnrepo为所创建仓库的路径,理论上可以是任何目录 ...
- Java与C/C++有什么区别
JDK包含JRE, 1-08: Helloworld: 01-08:classpath配置: 运行其它目录下的class文件: classpath一般不加分号,只找classpath下的文件: 后面加 ...
- 用XAML做网页!!—页头
原文:用XAML做网页!!-页头 接续上次进度,我们此次来制作页头. 首先要实现两侧边缘的美化,如下图所示: 在边缘处有一层朦胧的亮度反光效果,这也是通过简单的渐变实现的,而且我们在后面的每个区块中都 ...
- JAVA的class打包成dll
一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . //注意这 ...
- Eclipse中的SVN的冲突解决方案详解
版本冲突原因: 假设A.B两个用户都在版本号为100的时候,更新了kingtuns.txt这个文件,A用户在修改完成之后提交kingtuns.txt到服务器,这个时候提交成功,这个时候kingtuns ...
- zabbix 监控特定进程
因为一些server上跑着一些重要程序,须要对它们进行监控,公司用的是zabbix监控,之前都是在zabbix中加入自己定义脚本对特定程序进行监控,近期看了zabbix的官方文档,发现原来强大的zab ...