hdu 1372 Knight Moves

5 篇文章 0 订阅
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

typedef struct Node
{
    int x, y, stp;
}Node;

int vid[9][9];
int dir[8][2] = {1, -2, -1, -2,
                 -2, -1, -2, 1,
                 -1, 2, 1, 2,
                 2, -1, 2, 1};
int ans, si, sj, ei, ej;

int check(int x, int y)
{
    if (x>0 && x<=8 && y>0 && y<=8)
        return 1;
    return 0;
}

int main()
{
    char c1[3], c2[3];
    while (cin>>c1)
    {
        sj = c1[0]-'a'+1;
        si = c1[1]-'0';
        cin>>c2;
        ej = c2[0]-'a'+1;
        ei = c2[1]-'0';

        memset(vid, 0, sizeof(vid));

        queue<Node> que;
        Node p;
        p.x = si, p.y = sj, p.stp = 0;
        que.push(p);
        vid[si][sj] = 1;

        while (!que.empty())
        {
            p = que.front();
            que.pop();

            if (p.x==ei && p.y==ej)
            {
                ans = p.stp;
                break;
            }

            for (int d = 0; d < 8; d++)
            {
                int cx = p.x+dir[d][0], cy = p.y+dir[d][1];
                if (check(cx, cy) && !vid[cx][cy])
                {
                    Node next;
                    next.x = cx, next.y = cy, next.stp = p.stp+1;
                    que.push(next);
                }
            }
        }

        cout<<"To get from "<<c1<<" to "<<c2<<" takes "<<ans<<" knight moves."<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值