POJ1915 - Knight Moves - 广度优先搜索
2014-07-16 09:11
267 查看
#include<stdio.h> #include<string.h> int n; int mark[305][305]; int to[8][2]={{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; struct { int x,y,id; }st,ed,queue[100000],now,next; int bfs() { memset(mark,0,sizeof(mark)); int fro,end; fro=end=0; st.id=0; queue[end++]=st; mark[st.x][st.y]=1; while(fro<end) { now=queue[fro++]; if(now.x==ed.x&&now.y==ed.y) { return now.id; } for(int i=0;i<8;i++) { next.x=now.x+to[i][0]; next.y=now.y+to[i][1]; if(next.x<0||next.x>=n||next.y<0||next.y>=n||mark[next.x][next.y]==1) { continue; } next.id=now.id+1; mark[next.x][next.y]=1; queue[end++]=next; } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); scanf("%d%d",&st.x,&st.y); scanf("%d%d",&ed.x,&ed.y); printf("%d\n",bfs()); } }
相关文章推荐
- Knight Moves--广度优先搜索
- C++广度优先搜索之Knight Moves
- zoj - 1091 - Knight Moves(广度优先地图记数法)
- zoj - 1091 - Knight Moves(广度优先地图记数法)
- POJ 1915 Knight Moves (广度搜索)
- 机试算法讲解: 第42题 广度优先搜索之我该如何倒可乐
- 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历1
- 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历
- 图的深度优先搜索和广度优先搜索的总结
- 广度优先搜索,图的遍历
- 广度优先搜索----有环图
- 广度优先搜索
- 广度优先搜索解决迷宫问题
- File&&文件搜索深度优先和广度优先
- 邻接矩阵的广度优先搜索和深度优先搜索
- 广度优先搜索练习之神奇的电梯
- 广度优先搜索练习之神奇的电梯
- 22.基于 邻接表 表示的 深度优先搜索dfs 和 广度优先搜索bfs
- 连通图遍历策略之广度优先搜索(C语言)
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)