搜索——1015

题意:国际象棋。给出起点和终点,问最少几步可以跳过去。

思路:找出从起点出发到达终点的所有路径路径,然后再从这些路径中寻找最短路径

scanf跳过空格读字符

#include <iostream>

#include <stdio.h>
#include <memory.h>
#include <queue>
using namespace std;


int xx[8] = {1, 2, 1, 2, -1, -2, -1, -2};
int yy[8] = {2, 1, -2, -1, 2, 1, -2, -1};


char c1, c2;
int b1, b2, x1, x2, y1, y2;
bool ch[10][10];


struct point
{
    int x, y, step;
}n, m;


int main()
{
    int i;
    while(cin>>c1>>b1>>c2>>b2)
    {
        x1 = c1 - 'a' + 1;
        x2 = c2 - 'a' + 1;
        y1 = b1;
        y2 = b2;
        memset(ch, false, sizeof(ch));
        n.x = x1; n.y = y1;
        n.step = 0;
        ch[n.x][n.y] = true;
        queue<point> P;
        P.push(n);
        while(!P.empty())
        {
            m = P.front();
            P.pop();
            if(m.x == x2 && m.y == y2) break;
            for(i = 0; i < 8; i++)
            {
                n.x = m.x + xx[i];
                n.y = m.y + yy[i];
                n.step = m.step + 1;
                if(n.x>0 && n.x<=8 && n.y>0 && n.y<=8 && !ch[n.x][n.y])
                {
                    ch[n.x][n.y] = true;
                    P.push(n);
                }
            }
        }
        printf("To get from %c%d to %c%d takes %d knight moves.\n",
                c1, y1, c2, y2, m.step);
    }


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值