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 ...
随机推荐
- SharePoint 2013的HTML5特性之响应式布局
今天偶然看到一本书<Pro SharePoint 2013 Branding and Responsive Web Development>,看到SharePoint 2013基于HTML ...
- Ajax改动购物车
1.购物车类的设计 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpeWVxaWFuZ2xkaA==/font/5a6L5L2T/fontsize/4 ...
- cocos2d-x 消类游戏,类似Diamond dash 设计
前几天刚刚在学习cocos2d-x,无聊之下自己做了一个类似Diamond dash的消类游戏,今天放到网上来和大家分享一下.我相信Diamond dash这个游戏大家都玩过,游戏的规则是这样的,有一 ...
- EF一次请求公用一个实例
应用场景: 我们在程序开发时,对数据库的操作是必不可少的部分,常规的做法是直接使用Using()语句块,在用完后立即释放连接资源,这种做法在桌面应用程序中毫无问题,但是在Web程序中,尤其是在当今大数 ...
- httpcomponents-client-4.4.x
Chapter 1. Fundamentals Prev Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...
- hdu 3790 (最短路径问题dijkstra)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...
- 基于.net开发chrome核心浏览器【一】
原文:基于.net开发chrome核心浏览器[一] 说明: 这是本系列的第一篇文章,我会尽快发后续的文章. 源起 1.加快葬送IE6浏览器的进程 世界上使用IE6浏览器最多的地方在中国 中国使用IE6 ...
- windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码
原文:windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码 关于silverlight和wp7(windows phone 7)是默认不支持gb2312 ...
- ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件
原文:ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件 Login控件它是属于Membership服务的一部分,必须配置Membership提供程 ...
- 【Android】应用启动画面
几乎所有的Android应用程序都会有一个启动画面,展示自己的LOGO,本版信息,或者更人性化一点的,在很长的加载信息中,变换一些显示的文字等,让无聊的等待时间添加点调味剂. 具体实现来说,应该创建一 ...