uva439 - Knight Moves
2015-02-09 23:34
274 查看
题目本身就是象棋里马走“日”,每次共8种走法,记录走过的格子,有限次数内能搜到目标格 ~~
思考思考!!! 动脑子 动脑子!!!
思考思考!!! 动脑子 动脑子!!!
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<string> using namespace std; int a[10][10],flag[10][10],count; int step[10][3] = { {1,2},{1,-2},{-1,2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}}; int BFS(int x1, int y1, int x2, int y2) { int tail = 1, head = 0, list[100][3], ct[100]; list[head][0] = x1; list[head][1] = y1; flag[x1][y1] = 1; ct[head] = 0; while(head < tail) { for(int i = 0; i<8; i++) { int fx = step[i][0] + list[head][0], fy = step[i][1] + list[head][1]; if(fx > 0 && fx <= 8 && fy > 0 && fy <= 8 && !flag[fx][fy]){ if(fx == x2 && fy == y2) return count = ct[head] + 1; list[tail][0] = fx; list[tail][1] = fy; ct[tail++] = ct[head]+1; flag[fx][fy] = 1; } } head++; } return 0; } int main() { int m,n; char km, kn; while(scanf("%c%d %c%d", &km, &m, &kn, &n) != EOF) { getchar(); memset(a, 0, sizeof(a)); memset(flag, 0, sizeof(flag)); int x1 = km - 'a' + 1, y1 = m, x2 = kn - 'a'+1, y2 = n; count = 0; count = BFS(x1, y1, x2, y2); printf("To get from %c%d to %c%d takes %d knight moves.\n",km,m,kn,n,count); } return 0; }
相关文章推荐
- UVA——439 Knight Moves
- UVa 439 & SDU 1372 - Knight Moves
- 骑士的移动(Knight Moves, UVa 439)
- UVA 439--Knight Moves
- hdu 1372&&uva 439 KnightMoves(bfs)
- UVA 439 - Knight Moves
- [uva-439]Knight Moves(BFS)
- uva439 - Knight Moves(BFS求最短路)
- Uva 439 - Knight Moves(骑士游历)
- 习题 6-4 UVA 439 Knight Moves 骑士的移动
- UVa439---Knight Moves(BFS应用)
- UVA439 knightMoves (A*启发搜索)
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- UVA 439 Knight Moves
- uva 439 - Knight Moves
- UVA 439 - Knight Moves
- UVa 439-Knight Moves
- UVA 439 骑士(Knight Moves ) 很基础的BFS
- uva 439 Knight Moves 骑士移动
- uva 439 Knight Moves