Knight move

来源:互联网 发布:mfc图形界面编程实例 编辑:程序博客网 时间:2024/06/05 06:29

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.<br>Of course you know that it is vice versa. So you offer him to write a program that solves the &quot;difficult&quot; part. <br><br>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. <br>

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. <br>

For each test case, print one line saying "To get from xx to yy takes n knight moves.". <br>

e2 e4<br>a1 b2<br>b2 c3<br>a1 h8<br>a1 h7<br>h8 a1<br>b1 c3<br>f6 f6<br>

To get from e2 to e4 takes 2 knight moves.<br>To get from a1 to b2 takes 4 knight moves.<br>To get from b2 to c3 takes 2 knight moves.<br>To get from a1 to h8 takes 6 knight moves.<br>To get from a1 to h7 takes 5 knight moves.<br>To get from h8 to a1 takes 6 knight moves.<br>To get from b1 to c3 takes 1 knight moves.<br>To get from f6 to f6 takes 0 knight moves.<br>

题意:给出开始目的与最终目的,国际象棋骑士的走法,走“日”字,则那就向8个方向寻址,直到到达最终目的所求的最少步数。

思路:广度优先搜索。

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<queue>
using namespace std;
int mark[9][9];
int direction[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};//方向数组,8个方向
typedef struct//注意此处的typedef,结构体加上这个语句,就可以像类一样来声明数据类型,此处给出位置的结构体,包括行列以及到此处的步数
{
    int x,y,count;
}node;
node start,finish;


int bfs()//广度搜索函数
{
     memset(mark,0,sizeof(mark));//初始化
    node m,n;
    queue<node>q;
    q.push(start);
    mark[start.x][start.y] = 1;//避免循环,走过就算1
    start.count = 0;
    while(!q.empty())
    {
        m = q.front();
        q.pop();
        if(m.x==finish.x&&m.y==finish.y)//如果到达终点则结束返回count
            return m.count;
        for(int i=0;i<8;++i)//开始走
        {


            n.x = m.x + direction[i][0];
            n.y = m.y + direction[i][1];
            if(mark[n.x][n.y])//未做过
                continue;
            if(n.x<1||n.x>8||n.y>8||n.y<1)//不能超出范围
                continue;
            mark[n.x][n.y] = 1;
            n.count = m.count +1;
            q.push(n);//放入队列
        }


    }
    return 0;




}
int main()
{
    char rowstart,rowfinish;
    int colstart,colfinish,x,y,sum;


    while(scanf("%c",&rowstart)!=EOF)//输入时注意本题的输入格式
    {
        scanf("%d",&colstart);
        getchar();
        scanf("%c%d",&rowfinish,&colfinish);
        getchar();
        start.x = rowstart-'a'+1;
        start.y = colstart;
        finish.x = rowfinish-'a'+1;
        finish.y = colfinish;
        sum = bfs();
        printf("To get from %c%d to %c%d takes %d knight moves.\n",rowstart,colstart,rowfinish,colfinish,sum);


    }


















}





0 0
原创粉丝点击