杭电1327 Knight Moves 马走日

来源:互联网 发布:java分解质因数 编辑:程序博客网 时间:2024/06/06 02:23

Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 12
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 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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.
//大意是指,有一个棋盘,纵坐标用a,b,c,d..表示,横坐标用1,2,3..表示,给你两个坐标m,n,计算按照马的走法,从m走到n最少走几步(马走日)


//用广搜,队列进行搜索,


#include <iostream>

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cstdlib>
#include<map>
using namespace std;
int n,m;
int a[10][10];
int b[8][2]={2,1,2,-1,-2,-1,-2,1,1,2,1,-2,-1,2,-1,-2};
struct node
{
    int x;
    int y;
    int bs;
};//结构体,存横纵坐标和步数
int x1,x2;
int y1,y2;
int ss(int x1,int y1)
{
    queue<node> q;
    node ma,mi;
    ma.x=x1;
    ma.y=y1;
    ma.bs=0;
    q.push(ma);
    while(!q.empty())
    {
        mi=q.front();//取队首元素
        q.pop();//出队(出队含义是指针指向对首的下一个,不能获取对首值)
        int i;
        for(i=0;i<8;i++)//八个方向走
        {
            ma.x=mi.x+b[i][0];
            ma.y=mi.y+b[i][1];
            if(ma.x<9&&ma.y<9&&ma.x>0&&ma.y>0&&a[ma.x][ma.y]==0)//判断是否超界和是否走过
            {
                if(ma.x==x2&&ma.y==y2)
                {
                 printf("To get from %c%d to %c%d takes %d knight moves.\n",x1+'a'-1,y1,x2-1+'a',y2,mi.bs+1);
                    return 0;
                }
                a[ma.x][ma.y]=1;
                ma.bs=mi.bs+1;
                q.push(ma);//入队
            }
        }
    }
}
int main()
{
    char n,m;
    while(~scanf("%c%d %c%d",&n,&y1,&m,&y2))
    {
        if(n==m&&y1==y2)
        {
            printf("To get from %c%d to %c%d takes 0 knight moves.\n",n,y1,m,y2);
        }
        else
        {




        int i,j;
        int p;
        for(i=0;i<=9;i++)
        for(j=0;j<=9;j++)
        {
            a[i][j]=0;
        }
        x1=n-'a'+1;
        x2=m-'a'+1;
       ss(x1,y1);


    }
    }
}
0 0
原创粉丝点击