HDU1372:Knight Moves(BFS)

Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 17

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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.

Source

University of Ulm Local Contest 1996


#include <iostream>
#include<deque>
#include<cstring>
#include<cstdio>

using namespace std;
struct node
{
    int x,y,num;
};
deque<node> s;
int dr[8][2]={{2,1},{2,-1},{1,2},{1,-2},{-2,-1},{-2,1},{-1,-2},{-1,2} };
int vis[10][10];
int ans,i,sx,sy,tx,ty;
char ch1[4],ch2[4];
void bfs()
{
    node p;
    p.x=sx;
    p.y=sy;
    p.num=0;
    s.clear();
    vis[sx][sy]=1;
    s.push_back(p);
    while(!s.empty())
    {
        node q=s.front();
        for(int i=0;i<8;i++)
        {
            int xx=q.x+dr[i][0];
            int yy=q.y+dr[i][1];
            if(xx>0 && xx<=8 && yy>0 && yy<=8 && !vis[xx][yy])
            {
                p.x=xx;
                p.y=yy;
                p.num=q.num+1;
                s.push_back(p);
                vis[xx][yy]=1;
                if (xx==tx && yy==ty) {ans=p.num; return;}
            }
        }
        s.pop_front();
    }
    return;
}
int main()
{
    while(~scanf("%s %s",&ch1,&ch2))
    {
       ans=0;
       sx=ch1[0]-‘a‘+1;
       sy=ch1[1]-‘0‘;
       tx=ch2[0]-‘a‘+1;
       ty=ch2[1]-‘0‘;
       memset(vis,0,sizeof(vis));
       if (sx!=tx || sy!=ty) bfs();
       printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,ans);
    }
    return 0;
}
时间: 2024-08-25 14:17:15

HDU1372:Knight Moves(BFS)的相关文章

HDU1372:Knight Moves(经典BFS题)

HDU1372:Knight Moves(BFS) Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 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 v

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y

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 visit

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

poj2243 &amp;amp;&amp;amp; hdu1372 Knight Moves(BFS)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: pid=1372">http://acm.hdu.edu.cn/showproblem.php? pid=1372 Problem Description A friend of you is doing research on t

POJ 1915 Knight Moves [BFS]

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

UVa439 Knight Moves (BFS求最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/19436分析:BFS跑一次最短路,状态转移有8个. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 struct Point { 7 char r, c; 8 Point(char r = ' ', char c = ' '): r(r), c(c) {}; 9

UVa 439 Knight Moves(BFS应用)

题意  求国际象棋中骑士从一个位置移东到另一个位置所需最少步数 基础的BFS应用 #include <bits/stdc++.h> using namespace std; int x[] = { -2, -1, -2, -1, 1, 2, 1, 2}; int y[] = { -1, -2, 1, 2, -2, -1, 2, 1}; int d[15][15], sx, sy, ex, ey; pair<int, int> q[105], t; int bfs() { int c

POJ 2243:Knight Moves(BFS)

可以理解为象棋中的马走“日”字形,从第一个位置到第二个位置所需的最短步数,简单的BFS 每走一步需判断一次是否到达目标点. 由于BFS写得不多,一直用DFS的思维理解,递归写一直溢出.超时~~ #include"cstdio" #include"iostream" #include"cstring" #include"queue" using namespace std; int dx[8]={-1,1,-2,2,-2,2,-