hdu1372Knight Moves
来源:互联网 发布:百度搜索引擎优化指南 编辑:程序博客网 时间:2024/06/14 23:44
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372
一个可以走8个方向。BFS求最短路
代码:
#include <cstdio>#include <cstring>#include <queue>using namespace std;int v[10][10];int stx,sty,enx,eny;struct node{ int x,y; int step;};node ans;node t;int dx[] = {-1,-2,-2,-1,1,2,2,1};int dy[] = {-2,-1,1,2,2,1,-1,-2};void bfs(){ memset(v,0,sizeof(v)); queue<node> q; node f; f.x = stx; f.y = sty; f.step = 0; q.push(f); while(!q.empty()) { node h = q.front(); q.pop(); if(h.x == enx && h.y == eny) { ans = h; return ; } for(int i = 0 ;i < 8;++i) { int tx = h.x + dx[i]; int ty = h.y + dy[i]; if(tx >= 1 && tx <= 8 && ty >= 1 && ty <= 8 && !v[tx][ty]) { t.x = tx; t.y = ty; t.step = h.step + 1; q.push(t); v[tx][ty] = 1; } } }}int main(){ char s[5],s1[5]; while(~scanf("%s%s",s,s1)) { stx = s[1] - '0'; sty = s[0] - 'a' + 1; enx = s1[1] -'0'; eny = s1[0] - 'a' + 1; if(stx == enx && sty == eny) { printf("To get from %s to %s takes 0 knight moves.\n",s,s1); continue; } bfs(); printf("To get from %s to %s takes %d knight moves.\n",s,s1,ans.step); } return 0;}
0 0
- hdu1372Knight Moves
- hdu1372Knight Moves
- hdu1372Knight Moves
- hdu1372Knight Moves(基本bfs)
- hdu1372Knight Moves bfs
- 搜索专题: HDU1372Knight Moves
- Knight Moves
- pku1915Knight Moves
- zoj1091_hdu1372_Knight Moves
- Knight Moves
- POJ2243Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Linux下 挂载 Samba共享目录 到本地
- Myeclipse新建类报错Creation of element failed
- iOS查看线程,打印线程的方法
- JavaScript获取CSS中样式的值
- Sublime Text 3 使用技巧
- hdu1372Knight Moves
- CodeForces 293EClose Vertices
- sp_lock
- 后台接收json对象
- C# 之泛型详解
- Android Studio使用ButterKnife 8.0.1无效的问题
- Java内存模型--彻底详解
- 蓝桥杯【决赛试题】星期几
- Quicksort