fjnu1835 Knight Moves

来源:互联网 发布:会计软件开发 编辑:程序博客网 时间:2024/04/29 23:00

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.
KEY:经典的宽搜题,不过做题前你至少也知道骑士是怎么走的吧……呵呵……先直走一步,然后斜着走一步;
Source:

#include
<iostream>
using namespace std;

struct node
{
    
int x;
    
int y;
}
;

class Queue
{
    
int front;
    
int rear;
    node elem[
1000];
public:
    Queue()
    
{
        front
=rear=1;
    }

    
void EnQueue(node x)
    
{
        elem[rear
++]=x;
    }

    node DeQueue()
    
{
        
return elem[front++];
    }

    
int Empty()
    
{
        
if(front==rear) return 1;
        
else return 0;
    }

}
;

node s,e;
int a[10][10];
int dx[10]={0,-1,-2,-2,-1,1,2,2,1};
int dy[10]={0,-2,-1,1,2,2,1,-1,-2};

int ok(int x,int y)
{
    
if(x>=1&&x<=8&&y>=1&&y<=8&&a[x][y]==0return 1;
    
else return 0;
}


int shorest(int sx,int sy,int ex,int ey)
{
    a[sx][sy]
=1;
    s.x
=sx;
    s.y
=sy;
    e.x
=ex;
    e.y
=ey;
    Queue Q;
    Q.EnQueue(s);
    node t,tmp;
    
while(!Q.Empty())
    
{
        t
=Q.DeQueue();
        
if(t.x==e.x&&t.y==e.y) break;
        tmp
=t;
        
for(int i=1;i<=8;i++)
        
{
            tmp.x
=t.x+dx[i];
            tmp.y
=t.y+dy[i];
            
if(ok(tmp.x,tmp.y))
            
{
                Q.EnQueue(tmp);
                a[tmp.x][tmp.y]
=a[t.x][t.y]+1;
            }

        }

    }

    
return a[e.x][e.y];
}


void init()
{
    
int i,j;
    
for(i=1;i<=8;i++)
        
for(j=1;j<=8;j++)
            a[i][j]
=0;
}


int main()
{
//    freopen("fjnu_1835.in","r",stdin);
    char ps[5],pe[5];
    
while(scanf("%s %s ",ps,pe)!=EOF)
    
{
        init();
        
int sx,sy,ex,ey;
        sx
=ps[0]-'a'+1;
        sy
=ps[1]-'0';
        ex
=pe[0]-'a'+1;
        ey
=pe[1]-'0';
        
int min=shorest(sx,sy,ex,ey)-1;
        cout
<<"To get from "<<ps<<" to "<<pe<<" takes "<<min<<" knight moves."<<endl;
    }

    
return 0;
}


        


    




 
原创粉丝点击