784 - Maze Exploration(bfs)
2014-02-12 20:38
323 查看
题目:784 - Maze Exploration
题目大意:类似走迷宫, 八个方向走,空格的表示可以走,‘X’不可以走,‘*’是起点。
解题思路:BFS;
题目大意:类似走迷宫, 八个方向走,空格的表示可以走,‘X’不可以走,‘*’是起点。
解题思路:BFS;
#include<stdio.h> #include<string.h> #include<queue> using namespace std; const int N = 35; const int M = 85; int t, k; char maze [M]; int visit [M]; int dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; struct node { int x, y; }; queue<node> q; void bfs(int x, int y) { visit[x][y] = 1; maze[x][y] = '#'; node n; n.x = x; n.y = y; q.push(n); while(!q.empty()) { n = q.front(); q.pop(); for(int i = 0; i < 8; i++) { int x = n.x + dir[i][0]; int y = n.y + dir[i][1]; if(x >= 0 && x < k && y >= 0 && y < strlen(maze[x])) { if(maze[x][y] == ' ' && !visit[x][y]) { visit[x][y] = 1; maze[x][y] = '#'; node n1; n1.x = x; n1.y = y; q.push(n1); } } } } } int main() { scanf("%d%*c", &t); while(t--) { memset(maze, 0, sizeof(maze)); memset(visit, 0, sizeof(visit)); k = 0; while(gets(maze[k])) { if(maze[k][0] == '_') break; k++; } int i, j; for(i = 0; i < k; i++) { for(j = 0; j < strlen(maze[i]); j++) if(maze[i][j] == '*') break; if(maze[i][j] == '*') break; } bfs(i, j); for(i = 0; i <= k; i++) printf("%s\n", maze[i]); } return 0; }
相关文章推荐
- uva 784(bfs)
- 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS
- 关于BFS和DFS
- 图的广度优先搜索(BFS)和深度优先搜索(DFS)
- HDU 1242(bfs)
- 1098: [POI2007]办公楼biu 链表+BFS
- 简单的BFS, 3D迷宫
- Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)
- HDU-1252 || ZOJ-1103 || POJ-2415 Hike on a Graph(bfs)
- POJ 3083 (bfs + dfs)
- [DFS/BFS]HOJ1797Red and Black
- POJ 1753(DFS+枚举||BFS+位运算)
- BFS模版程序
- sdjzu1102 迷宫问题(BFS)
- POJ 3278(BFS)
- HDOJ 3468 Treasure Hunting (BFS+DFS+匈牙利算法) 综合性好题,好题!!!
- 广度优先搜索(BFS)
- nyoj 523 亡命逃窜 【BFS】
- ZOJ 3675 Trim the Nails(bfs)
- bfs