RQNOJ 195校园迷宫(简单BFS)
2012-05-14 12:40
281 查看
/* * 简单BFS */ #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 2005; int map ; bool vis ; struct node { int x; int y; int c; }Q[N*N], s, e; int front, rear; int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int BFS(int n, int m) { node first, next; memset(vis, true, sizeof(vis)); front = rear = 0; vis[s.x][s.y] = false; Q[front++] = s; while (rear < front) { first = Q[rear++]; if (first.x==e.x && first.y==e.y) return first.c; for (int i=0; i<4; ++i) { next.x = first.x + dir[i][0]; next.y = first.y + dir[i][1]; next.c = first.c + 1; if (next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]&&map[next.x][next.y]==0) { Q[front++] = next; vis[next.x][next.y] = false; } } } return -1; } int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { for (int i=0; i<n; ++i) { for (int j=0; j<m; ++j) scanf ("%d", &map[i][j]); } scanf ("%d%d%d%d", &s.x, &s.y, &e.x, &e.y); --s.x, --s.y, --e.x, --e.y; s.c = 0; int ans = BFS(n, m); if (ans < 0) printf ("No Answer!\n"); else printf ("%d\n", ans); } return 0; }
相关文章推荐
- 校园迷宫-rqnoj-195
- BFS简单题套路_Codevs 1215 迷宫
- poj3984 迷宫问题 简单bfs打印路径
- bfs简单题 迷宫问题
- (简单) POJ 3984 迷宫问题,BFS。
- EOJ 1224 简单迷宫问题 bfs
- hdu 1728 逃离迷宫[简单bfs]
- 迷宫的最短路径(BFS的简单应用)
- RQNOJ 校园迷宫-宽度优先搜索
- poj 3984 迷宫问题【BFS && DFS】【简单】
- RQNOJ 34紧急救援(简单BFS)
- 简单的迷宫(bfs)noj1793
- BFS - 最简单的迷宫问题
- 最简单的BFS入门题目——迷宫的最短路径
- 蓝桥杯 算法提高 学霸的迷宫(简单bfs+记录路径)
- BFS简单迷宫
- 【图论】【RQNOJ】校园迷宫
- BFS之最简单的迷宫问题(并打印路径)
- 【BFS】【Rqnoj195】
- nyoj82 迷宫寻宝(一) (bfs)