NYoj 58 最小的步数 (BFS
2017-03-22 16:27
176 查看
最少步数
时间限制:3000 ms | 内存限制:65535 KB难度:4
描述
这有一个迷宫,有0~8行和0~8列:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,1
0表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
输入
第一行输入一个整数n(0
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define N 1000 bool vis ; int dir[4][2] = {0,1,0,-1,1,0,-1,0}; int m, n, sx, sy, ex, ey; int map[9][9]= { 1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1, }; struct node { int x; int y; int step; bool friend operator<(node a,node b) { return a.step > b.step; // 最小堆 } }st,mid,ne; bool judge(int x,int y) { if(x>=0 && x<9 && y>=0 && y<9) return true; else return false; } int bfs() { priority_queue<node> fq; st.x = sx; st.y = sy; st.step = 0; fq.push(st); vis[sx][sy] = true; while(!fq.empty()) { mid = fq.top(); fq.pop(); if(mid.x==ex && mid.y==ey) return mid.step; for(int i = 0;i < 4; i++) { ne.x = mid.x + dir[i][0]; ne.y = mid.y + dir[i][1]; if(ne.x==ex && ne.y==ey) return mid.step + 1 ; if(judge(ne.x,ne.y) && map[ne.x][ne.y]!=1 && !vis[ne.x][ne.y]) { ne.step = mid.step + 1; vis[ne.x][ne.y] = true; fq.push(ne); } } } return 0; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&sx,&sy,&ex,&ey); memset(vis,false,sizeof(vis)); int k = bfs(); printf("%d\n",k); } return 0; }
相关文章推荐
- nyoj 58 最小步数 dfs与bfs均可
- NYOJ 58 最小步数(BFS && DFS)
- nyoj58最小步数——BFS
- NYOJ ~ 58 ~ 最小步数(BFS)
- NYOJ 58 最小步数(BFS)
- nyoj 58 最小步数(bfs)
- NYOJ 58 最小步数【bfs】
- NYOJ ~ 58 ~ 最小步数(BFS)
- NYOJ 58 最小步数(bfs)
- nyoj——58 最小步数(DFS,BFS 迷宫问题)
- nyoj 58 最小步数(入门bfs)
- nyoj 58 最小步数问题 BFS
- NYOJ 58 最小步数 BFS queue 实现
- 搜索学习(2)--NYOJ 58 最小步数
- 最小步数 n 58 bfs
- NYOJ-58 最小步数 AC 分类: NYOJ 2014-01-22 22:01 217人阅读 评论(0) 收藏
- 最小步数 n 58 bfs
- nyoj27水池数目VSnyoj58最小步数(DFS)
- NYOJ-58 最小步数 AC
- nyoj 58 最小步数