poj 1372 Knight Moves

来源:互联网 发布:编程与机械 编辑:程序博客网 时间:2024/05/21 15:47

Knight Moves

                         Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
                         Total Submission(s): 3763 Accepted Submission(s): 2344


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.
 
 
题意:给一个N*N正方形棋盘,再给开始点和终点,求从开始到最终点的最短经过的步数,方向是八个方向都可以,int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
 
解题: 用BFS进行 广度优先搜索…… (开始的时候因为N被从定义了 所以数据一直没有输出来 下回应该注意局部变量和全局变量的定义……)
 
 
代码:
 
#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<queue>#include<stdlib.h>using namespace std;char sta[3],end[3];int  sx,sy,ex, ey;const int manx=100;char s[3],e[3];bool visit[manx][manx];  //访问标记int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1}; //方向向量struct State     // bfs状态数据结构{    int x,y;    //坐标位置    int step    // 搜索步数统计器    ;}a[manx],st;bool checkstate(State st)    // 约束条件检测{    if (st.x<=8&&st.x>=1&&st.y<=8&&st.y>=1)        return 1;    else                 // 约束条件冲突            return 0;}void output(int n){    cout << "To get from " << s << " to " << e << " takes " << n << " knight moves." << endl;}void bfs(){    queue<State> q;                  //BFS队列    memset(visit,0, sizeof(visit));    State now,next;                 // 定义2个状态,当前和下一个    st.step=0;                     // 计数器清零    q.push(st);                      // 入队    visit[st.x][st.y]=1;             //访问标记    while (!q.empty())    {        now=q.front();             //取对首元素进行扩展        q.pop();                   //队首元素出队        if (now.x == ex && now.y == ey)    //出现目标态时,step的最小值,可以退出        {            output(st.step);            return ;        }        for (int i=0;i<8;i++)        {            next.x=now.x+dir[i][0];         // 按照规则生成下一个状态            next.y=now.y+dir[i][1];            next.step=now.step+1;            if (next.x == ex && next.y == ey)  //再一次if判断 很重要            {                output(next.step);                return ;            }            if (checkstate(next))     // 如果状态满足约束条件入队            {                q.push(next);                visit[next.x][next.y]=1;            }        }    }}int main(){    while (cin>>s>>e)    {        sx=s[0]-'a'+1;        sy=s[1]-'0';        ex=e[0]-'a'+1;        ey=e[1]-'0';        st.x=sx;        st.y=sy;        bfs();    }    return 0;}

原创粉丝点击