HDOJ/HDU 1372 Knight Moves(经典BFS)

Problem Description
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.

Input
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
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.

题意:国际象棋的骑士~可以理解成象棋中的马,走日字。
行号从:1-8
列号从:a-h
问:从起点到终点的最短路径是几步。

遇到最短路径的题。最好用广搜,虽然深搜也可以AC。

结构体变量名不要取next,否则会出现CE!!!
这个next搞了我个把小时,后来改成nextb,就AC了。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int xa,ya,xb,yb;
char a[3],b[3];
struct node{
    int x;
    int y;
    int t;
}first,nextb;
int map[10][10];
int dir[8][2]={-2,1,-2,-1,-1,2,-1,-2,1,2,1,-2,2,-1,2,1};

void bfs(){
    int i;
    queue<node> q;
    first.x=xa;
    first.y=ya;
    first.t=0;
    q.push(first);
    map[first.x][first.y]=1;
    while(!q.empty()){
        first = q.front();
        //printf("---%d---%d\n",first.x,first.y);
        q.pop();
        if(first.x==xb&&first.y==yb){
            printf("To get from %s to %s takes %d knight moves.\n",a,b,first.t);
            return;
        }
        for(i=0;i<8;i++){
            nextb.x=first.x+dir[i][0];
            nextb.y=first.y+dir[i][1];

            if(nextb.x<0||nextb.x>=8||nextb.y<0||nextb.y>=8){
                //printf("1\n");
                continue;
            }
            if(map[nextb.x][nextb.y]==1){
                //printf("2\n");
                continue;
            }

            map[nextb.x][nextb.y]=1;
            nextb.t=first.t+1;
            q.push(nextb);
        }
    }
}

int main()
{

    while(~scanf("%s%s",&a,&b)){
        xa=a[0]-'a';
        ya=a[1]-'1';
        xb=b[0]-'a';
        yb=b[1]-'1';

        memset(map,0,sizeof(map));
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                map[i][j]=0;
            }
        }
        //printf("a=%s\n",a);
        //printf("b=%s\n",b);
        bfs();
    }
    return 0;
}
时间: 2024-10-06 12:22:02

HDOJ/HDU 1372 Knight Moves(经典BFS)的相关文章

POJ 1915 Knight Moves 双向BFS 入门

 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the minimum number o

UVa 439:Knight Moves 搜索专题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=380 题目类型: 搜索 样例输入: e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6 样例输出: To get from e2 to e4 takes 2 knight moves. To get fr

uva 439 - Knight Moves

点击打开链接 题目意思:有一个8x8的棋盘,初始给定两个位置,求出从第一个位置到第二个位置的最短路 解题思路:对于这一类的求最短路我们一般用广搜来做,开个结构体存储坐标,然后用队列存储这个这个结构体的对象,开始把第一个点放入队列,接下来进行BFS,注意这一题最大的mark标记数组开到750左右,我因为开了1010的数组TLE到蛋疼啊,不懂是不是因为数据实在很多然后每一次都调用memset还有其它的耗时大. 代码: #include <iostream> #include <queue&g

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa

HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)

Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的. Input 测试数据有多组,每组的表

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 1465 不容易系列之一(错排)

Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样. 话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的.比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然把40个单项选择题全部做错了!大家都学过概率论,应该知道出现这种情况的概率,所以至今我都觉得这是一件神奇的事情.如果套用一句经典的评语,我们可以这样总结

hdu 2612 Find a way (BFS)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6758    Accepted Submission(s): 2253 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco