UVA 439 Knight Moves

UVA-439

题意:给出两个坐标,求骑士从从第一个条到第一个坐标跳(中国象棋中马的走法)到第二个坐标要走多少步。
解题思路:一般遇到求最少步骤的就会先想到bfs。简单的广搜题目。

/*************************************************************************
    > File Name: UVA-439.cpp
    > Author: Narsh
    > 
    > Created Time: 2016年07月20日 星期三 15时17分30秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
    int x,y,l;
}q[600000];
const int MoVe[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
int n,m,h,t,x,y;
bool map[30][30];
string s;
int main() {
//cout<<"s  b"<<endl;
    while (cin>>s) {
        h=t=0;
        memset(map,true,sizeof(map));
        q[++t].x=s[0]-'a'+1;
        q[t].y=s[1]-'0';
        q[t].l=0;
        map[q[t].x][q[t].y]=false;
        printf("To get from %c%c to ",s[0],s[1]);
        cin>>s;
        printf("%c%c takes ",s[0],s[1]);
        n=s[0]-'a'+1;
        m=s[1]-'0';
        while (h < t) {
            h++;
            if (q[h].x == n && q[h].y == m) {
                printf("%d knight moves.\n",q[h].l);
                break;
            }
            for (int i = 0; i < 8; i++) {
                x=q[h].x+MoVe[i][0];
                y=q[h].y+MoVe[i][1];
                if (1 <= x && x <= 8 && 1 <= y && y <= 8 && map[x][y]) {
                    map[x][y]=false;
                    t++;
                    q[t].x=x;
                    q[t].y=y;
                    q[t].l=q[h].l+1;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值