Knight Moves

option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=380" target="_blank" style="text-decoration:none">From:UVA,
439

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)的更多相关文章

  1. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  2. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

  3. uva 439 Knight Moves 骑士移动

    这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  4. 【UVa】439 Knight Moves(dfs)

    题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <a ...

  5. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  6. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  7. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  8. HDU1372:Knight Moves(BFS)

    Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  9. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...

随机推荐

  1. 手机游戏产品经理(一)logo的印象非常重要,以促进

    从事的工作有一段时间的产品,在产品上共享所以现在的一些经验和知识,并记录.首先,我现在做国外casino手游,如此专注casino展开游戏的主题. 首先说一款游戏的logo非常重要,假设设计的好.它能 ...

  2. C#List<long>与String(Linq)

    1.string => List<long> string ShopIds = "1,2,3"; var ShopIdList = ShopIds.Split(' ...

  3. A Game of Thrones(5) - Jon

    There were times—not many, but a few—when Jon Snow was glad he was a bastard. As he filled his wine ...

  4. Ubuntu12.04下载Repo

    操作系统:Ubuntu12.04LTS 64bit "#"号后面表示凝视内容 $cd ~ #进入下载文件夹 $mkdir bin #创建bin文件夹用于存储Repo脚本 $PATH ...

  5. windows phone 7 通过Post提交URL到服务器,从服务器获取数据(比如登陆时候使用)

    原文:windows phone 7 通过Post提交URL到服务器,从服务器获取数据(比如登陆时候使用) HttpWebRequest myRequest = (HttpWebRequest)Web ...

  6. 设计模式之迭代器模式(Iterator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  7. 神经网络BP算法C和python代码

    上面只显示代码. 详BP原理和神经网络的相关知识,请参阅:神经网络和反向传播算法推导 首先是前向传播的计算: 输入: 首先为正整数 n.m.p.t,分别代表特征个数.训练样本个数.隐藏层神经元个数.输 ...

  8. Struts 2 初学的复习巩固

    Q:使用Struts2 开发程序的基本步骤? A: 1)加载Struts2类库: 2)配置web.xml文件,定义核心Filter来拦截用户请求: 3)开发视图层页面,即JSP页面: 4)定义处理用户 ...

  9. Windows Phone开发(25):启动器与选择器之WebBrowserTask

    原文:Windows Phone开发(25):启动器与选择器之WebBrowserTask 从名字上就看出来,这个家伙就是打开浏览并浏览到指定页面. 它有两个用途完全一样的属性:Uri属性是Syste ...

  10. Windows Phone开发(12):认识一下独具个性的磁贴

    原文:Windows Phone开发(12):认识一下独具个性的磁贴 对"磁贴"的理解是一点也不抽象的,为什么呢?只要你愿意启动WP系统,无论你是在模拟器中还是在真机中,是的,桌面 ...