【HDOJ】1484 Basic wall maze
2015-02-21 12:10
260 查看
BFS。
/* 1484 */ #include <iostream> #include <queue> #include <string> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef struct { int x, y; string s; } node_t; bool m[8][8][4]; bool visit[8][8]; char ds[5] = "NSWE"; int dir[4][2] = { -1,0,1,0,0,-1,0,1 }; const int n = 6; int bx, by, ex, ey; string ans; void bfs() { int x = bx, y = by; int i, j, k; queue<node_t> Q; string s; node_t nd, tmp; memset(visit, false, sizeof(visit)); visit[x][y] = true; nd.x = x; nd.y = y; nd.s = ""; Q.push(nd); while (!Q.empty()) { nd = Q.front(); Q.pop(); for (i=0; i<4; ++i) { if (m[nd.x][nd.y][i]) continue; x = nd.x + dir[i][0]; y = nd.y + dir[i][1]; if (x<=0 || x>6 || y<=0 || y>6 || visit[x][y]) continue; visit[x][y] = true; tmp.x = x; tmp.y = y; tmp.s = nd.s + ds[i]; if (x==ex && y==ey) { ans = tmp.s; return ; } Q.push(tmp); } } } int main() { int i, j, k; int x, y; int a, b, c, d; #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif while (scanf("%d %d",&by,&bx)!=EOF && (by||bx)) { scanf("%d %d", &ey, &ex); memset(m, false, sizeof(m)); for (i=0; i<3; ++i) { scanf("%d%d%d%d", &a,&b,&c,&d); if (b == d) { // north & south need to mask for (y=a+1; y<=c; ++y) { m[b][y][1] = true; m[b+1][y][0] = true; } } else { // west & east need to mask for (x=b+1; x<=d; ++x) { m[x][a][3] = true; m[x][a+1][2] = true; } } } bfs(); printf("%s\n", ans.c_str()); } return 0; }
相关文章推荐
- HDU 1484 Basic wall maze (dfs + 记忆)
- HDU 1484 Basic wall maze
- HDU 1484 Basic wall maze (dfs + 记忆化)
- hdu 1484 Basic wall maze bfs + 输出路径
- (BFS)poj2935-Basic Wall Maze
- POJ:2935 Basic Wall Maze
- poj 2935 Basic Wall Maze
- poj_2935 Basic Wall Maze
- POJ 2935 Basic Wall Maze (BFS)
- poj 2935 Basic Wall Maze
- poj 2935 Basic Wall Maze
- Basic Wall Maze
- POJ-2935 Basic Wall Maze
- Basic Wall Maze_条件bfs_2018_2_23
- POJ2935 Basic Wall Maze(BFS)
- poj2935 Basic Wall Maze
- Basic Wall Maze
- poj 2935 Basic Wall Maze bfs
- POJ 2935 Basic Wall Maze
- 22 poj2935 Basic Wall Maze --- bfs