HDU 1372 Knight Moves(BFS)

原创 2016年05月30日 09:54:47
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.

简单广搜,走的是日字。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char a,c;
int b,d;
int vis[9][9];
int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
struct node
{
    int x,y,step;
}pose;
void bfs()
{
    queue<node>Q;
    vis[pose.x][pose.y]=1;
    Q.push(pose);
    while(!Q.empty())
    {
        node p=Q.front();
        Q.pop();
        if(p.x==c-'a'+1&&p.y==d)
        {
            printf("To get from %c%d to %c%d takes %d knight moves.\n",a,b,c,d,p.step);
            return;
        }
        for(int i=0;i<8;i++)
        {
            node q=p;
            q.x+=f[i][0];
            q.y+=f[i][1];
            q.step+=1;
            if(q.x<1||q.x>8||q.y<1||q.y>8)
                continue;
            if(!vis[q.x][q.y])
            {
                vis[q.x][q.y]=1;
                Q.push(q);
            }
        }
    }
}
int main()
{
    while(cin>>a>>b)
    {
        getchar();
        cin>>c>>d;
        pose.x=a-'a'+1;
        pose.y=b;
        pose.step=0;
        memset(vis,0,sizeof(vis));
        bfs();
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ---2243 Knight Moves 使用A*算法的广度优先搜索

一个简单的A星算法实例。
  • xiaozhuaixifu
  • xiaozhuaixifu
  • 2013年07月02日 14:09
  • 1049

HDU/HDOJ 1372 Knight Moves(骑士游走问题) 简单广度优先搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 思路:8个方向依次入队即可,只需标记访问,广度优先搜索是按照层次来搜索,如果存在一条通路,那么...
  • xiaozhuaixifu
  • xiaozhuaixifu
  • 2013年06月03日 22:58
  • 1236

hdu1372 dfs搜索之国际象棋的马

中国象棋和国际象棋应该还是有些渊源的,的确二者的起源不同。中国象棋在战国以前就有了,而国际象棋流行的说法是起源于古印度。二者在千百年的历史长河中,其规则和形态也都是在一直变化的。中国古代很早就和印度有...
  • guodongxiaren
  • guodongxiaren
  • 2014年07月05日 12:38
  • 1657

HDU 1372 Knight Moves(BFS)

HDU1372:Knight Moves(BFS) Problem Description A friend of you is doing research on the Tra...
  • u013497977
  • u013497977
  • 2014年12月18日 23:15
  • 361

HDU1372:Knight Moves(BFS)

Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y...
  • libin56842
  • libin56842
  • 2013年06月03日 20:11
  • 5035

HDU1372 Knight Moves(广搜BFS+深搜DFS)

题目: Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • riba2534
  • riba2534
  • 2017年01月08日 11:09
  • 191

HDU1372 Knight Moves(BFS)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Pr...
  • wust_xhj
  • wust_xhj
  • 2015年08月16日 13:39
  • 236

poj2243 && hdu1372 Knight Moves(BFS)

poj2243&&hdu1372 Knight Moves(BFS)
  • u012860063
  • u012860063
  • 2014年07月14日 15:02
  • 859

【hdu 1372】 Knight Moves (BFS)

Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot...
  • zfz1015
  • zfz1015
  • 2012年08月13日 20:04
  • 358

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

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223...
  • u013487051
  • u013487051
  • 2014年07月16日 18:50
  • 709
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 1372 Knight Moves(BFS)
举报原因:
原因补充:

(最多只允许输入30个字)