您的位置:首页 > 其它

UVA-439 Knight Moves

2016-08-08 19:21 288 查看

UVA-439 Knight Moves

题目大意:给出两个坐标,求骑士从从第一个条到第一个坐标跳(中国象棋中马的走法)到第二个坐标要走多少步

解题思路:这题看同学题解知道用广搜来做 于是去学习了一下广搜, 每次需找这一步的所有下一步 有没有符合的 没有则 继续寻找每个下一步的下一步有没符合的 一直到找到或者没有找到即可

/*************************************************************************
> File Name: UVA-439.cpp
> Author: Robin
> Mail: 499549060@qq.com
> Created Time: 2016年08月08日 星期一 15时14分42秒
************************************************************************/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int d[8][2] = {1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
struct weizhi{
int x;
int y;
int l;
}q[60000];
int map[30][30];
int main() {
string s;
while (cin>>s) {
memset(map, 1, sizeof(map));
printf("To get from %c%c",s[0],s[1]);
int t1 = 0;
int t2 = 0;
q[t1].x = s[0] - 'a' + 1;
q[t1].y = s[1] - '0';
q[t1].l = 0;
map[q[t1].x][q[t1].y] = 0;
t1++;
cin>>s;
printf(" to %c%c takes ",s[0], s[1]);
int n,m;
n = s[0] - 'a' + 1;
m = s[1] - '0' ;
while (t2 < t1 )  {
if ( q[t2].x == n && q[t2].y == m) {
printf("%d knight moves.\n",q[t2].l);
break;
}
for (int i = 0; i < 8; i++) {
int x = q[t2].x + d[i][0];
int y = q[t2].y + d[i][1];
if (map[x][y] && x >0 && x <= 8 && y > 0 && y <= 8) {
map[x][y] = 0;
q[t1].x = x;
q[t1].y = y;
q[t1].l = q[t2].l + 1;
t1++;
}
}
t2++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: