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++; } } }
相关文章推荐
- uva 439 - Knight Moves
- UVA439 - Knight Moves
- UVA 439(p177)----Knight Moves
- UVA - 439 - Knight Moves (BFS)
- 【习题 6-4 UVA-439】Knight Moves
- uva 439 knight moves
- UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves
- UVA - 439 Knight Moves
- uva 439 Knight Moves
- UVA_439 Knight Moves
- uva_439 - Knight Moves
- UVA - 439 Knight Moves
- UVA 439 Knight Moves
- UVA - 439 Knight Moves
- uva 439 - Knight Moves
- UVa 439 - Knight Moves 搜索专题
- UVA 439 Knight Moves
- Uva 439 Knight Moves
- UVa 439,Knight Moves
- UVA 439 - Knight Moves