宽度优先搜索1185: Knight Moves

来源:互联网 发布:2017最流行的编程语言 编辑:程序博客网 时间:2024/05/29 18:37
ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE1s8192K622290Standard

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 visitseach square of a given set ofn 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 solvesthe "difficult" part.

Your job is to write a program that takes two squares a and bas input and then determines the number of knight moves on a shortest routefroma to b.

Input Specification

The input file will contain one or more test cases. Each test case consists of one linecontaining 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 Specification

For each test case, print one line saying "To get from xx to yy takesn 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.第一次用bfs写题,有点麻烦,以后改进吧#include <stdio.h>#include <string.h>#include <queue>#include <iostream>using namespace std;int dist[10][10];                                   //记录距离 bfs必备int vis[10][10];                                    //记录是否算过void bfs (int n,int m){    queue<int>  q;    q.push(n);    int a,b;    a=q.front()/8;      b=q.front()%8;    if(b==0) {a-=1;b=8;}                      //一直在这出错。。。。    vis[a][b]=1;    dist[a][b]=0;    while(q.front()!=m)    {        int a,b;        a=q.front()/8;        b=q.front()%8;        if(b==0) {a-=1;b=8;}        if(a-1>=1&&b-2>=1&&!vis[a-1][b-2]) {  dist[a-1][b-2]=dist[a][b]+1; vis[a-1][b-2]=1; q.push((a-1)*8+(b-2));}        if(a+1<=8&&b-2>=1&&!vis[a+1][b-2]) {  dist[a+1][b-2]=dist[a][b]+1; vis[a+1][b-2]=1; q.push((a+1)*8+(b-2));}        if(a+1<=8&&b+2<=8&&!vis[a+1][b+2]) {  dist[a+1][b+2]=dist[a][b]+1; vis[a+1][b+2]=1; q.push((a+1)*8+(b+2));}        if(a-1>=1&&b+2<=8&&!vis[a-1][b+2]) {  dist[a-1][b+2]=dist[a][b]+1; vis[a-1][b+2]=1; q.push((a-1)*8+(b+2));}        if(b-1>=1&&a-2>=1&&!vis[a-2][b-1]) {  dist[a-2][b-1]=dist[a][b]+1; vis[a-2][b-1]=1; q.push((a-2)*8+(b-1));}        if(b+1<=8&&a-2>=1&&!vis[a-2][b+1]) {  dist[a-2][b+1]=dist[a][b]+1; vis[a-2][b+1]=1; q.push((a-2)*8+(b+1));}        if(b+1<=8&&a+2<=8&&!vis[a+2][b+1]) {  dist[a+2][b+1]=dist[a][b]+1; vis[a+2][b+1]=1; q.push((a+2)*8+(b+1));}        if(b-1>=1&&a+2<=8&&!vis[a+2][b-1]) {  dist[a+2][b-1]=dist[a][b]+1; vis[a+2][b-1]=1; q.push((a+2)*8+(b-1));        q.pop();    }}int main (){    char a,b,c,d;    int e,f;    while(scanf("%c%d%c%c%d%c",&a,&e,&c,&b,&f,&d)==6)    {        //memset(fa,0,sizeof(fa));        memset(dist,0,sizeof(dist));        memset(vis,0,sizeof(vis));        int n=(a-'a'+1)*8+e;        int m=(b-'a'+1)*8+f;        bfs(n,m);        int x,y;        x=m/8;        y=m%8;        if(y==0) {x-=1;y=8;}        printf("To get from %c%d to %c%d takes %d knight moves./n",a,e,b,f,dist[x][y]);    }    return 0;}