POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11223 Accepted: 6331

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

数组不要开的太大。。还要记得将队列清空。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>

using namespace std;

const int N = 200;
int n , m;
char color[N][N];
int vist[N][N];

int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int xx;
int yy;
int ans;
struct node
{
    int x;
    int y;
    char c;
    int num;
};
queue<node>q;
node a, b;

void bfs()
{
   vist[a.x][a.y]=1;
    while( !q.empty() )
    {
        node temp = q.front();
        if( temp.x==b.x && temp.y==b.y )
        {
            printf("To get from %c%d to %c%d takes %d knight moves.\n", a.c, a.y, b.c, b.y, temp.num);
            return ;
        }
        q.pop();
        for(int i=0; i<8; i++)
        {
            xx = temp.x + dx[i];
            yy = temp.y + dy[i];
            if( xx<1 || xx>8 || yy<1 || yy>8 || vist[xx][yy])
                continue;
            node next;
            next.x = xx;
            next.y = yy;
            next.num=temp.num+1;
            vist[xx][yy]=1;
            q.push( next );
        }
    }
}

int main()
{
    int i,j;
    while(~scanf("%c%d %c%d", &a.c, &a.y, &b.c, &b.y))
    {
        getchar();
        while( !q.empty() ) q.pop();
        memset( vist, 0, sizeof(vist) );
        a.x = a.c - 'a'+1;
        b.x = b.c - 'a'+1;
        node first;
        first.x = a.x;
        first.y = a.y;
        first.num=0;
        ans = 0;
        q.push( first );
        bfs();
    }

    return 0;
}

POJ 2243 || HDU 1372:Knight Moves(BFS),布布扣,bubuko.com

时间: 2024-10-20 17:22:21

POJ 2243 || HDU 1372:Knight Moves(BFS)的相关文章

HDU - 1372 Knight Moves(bfs入门)

HDU - 1372 Knight Moves 题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567 题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法. 输入: 输入包含一个或多个输入样例.每个测试样例将会有两个坐标,表示现在的位置和将要到达的地

HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10372    Accepted Submission(s): 6105 Problem Description A friend of you is doin

HDU 1372 Knight Moves(bfs)

嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向,然后从起点到终点跑一遍最典型的bfs即可...注意HDU的坑爹输入和输出... AC代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<cstring> 5 6 usi

HDU 1372 Knight Moves【BFS】

题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次[email protected][email protected]) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue&

HDU 1372.Knight Moves

Knight Moves Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1372 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour

HDU 1372 Knight Moves 题解

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14125    Accepted Submission(s): 8269 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe

HDOJ 1372 Knight Moves【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7482    Accepted Submission(s): 4481 Problem Description A friend of you is doin

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

杭电 1372 Knight Moves(广搜模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6439    Accepted Submission(s): 3886 Problem Description A friend of you is doing res