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. Effective C++ -- 构造析构赋值运算

    05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...

  2. String的split

    对于  http://10.13.30.22/svn/SVNRepository/UnChecked/Test  想要分割他就要用: String subContent[]=modelInfo.get ...

  3. 7款开源Java反编译工具

    今天我们要来分享一些关于Java的反编译工具,反编译听起来是一个非常高上大的技术词汇,通俗的说,反编译是一个对目标可执行程序进行逆向分析,从而得到原始代码的过程.尤其是像.NET.Java这样的运行在 ...

  4. pygame系列_draw游戏画图

    说到画图,pygame提供了一些很有用的方法进行draw画图. ''' pygame.draw.rect - draw a rectangle shape draw a rectangle shape ...

  5. UVa 103 - Stacking Boxes (LIS,打印路径)

    链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...

  6. PHP获取当前页面完整的URL

    #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br> ...

  7. UINavigationController的横屏问题

    近期用代码创建了一个UINavigationController,并且当前的屏幕设置为横屏的,此时遇到的问题是UINavigationController的view的大小为宽768 高1024,也就是 ...

  8. 多线程——达到Runnable介面

    部分博客(多线程--继承Thread类)介绍了java多线程的第一种实现方法--继承Thread类.这篇博客介绍另外一种方法--实现Runnable接口,并实现run方法. 还用上篇博客的样例.如今用 ...

  9. 状态压缩dp入门

    poj1321 http://poj.org/problem?id=1321 我们可以把棋盘的每一行看做是一个状态,如果某一列放置了棋子,那么就标记为1,否则就标记为0.然后把它看成是一个二进制数,然 ...

  10. javascript - 浏览TOM大叔博客的学习笔记

    part1 ---------------------------------------------------------------------------------------------- ...