poj1475
来源:互联网 发布:java程序员如何做兼职 编辑:程序博客网 时间:2024/06/03 14:14
推箱子,嵌套BFS, 比较直白
稍微详细一点的解释见这里
http://www.xuebuyuan.com/304871.html
#include<cstdio>#include<cstring>#include<iostream>#include<queue>using namespace std;#define MAXN 25int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};int vis_box[MAXN][MAXN], vis_player[MAXN][MAXN];char map[MAXN][MAXN];int r, c, px, py, bx, by;char box_path[5] = "NWSE";char player_path[5] = "nwse";string temp_ans;struct Player{ int x, y; string path;};struct Box{ int x, y; int px, py; string path;};bool ok(int x, int y) { if(x >= 0 && x < r && y >= 0 && y < c) { return true; } return false;}bool Player_BFS(int sx, int sy, int ex, int ey, int tx, int ty) { temp_ans = ""; if(ex < 0 || ex >= r || ey < 0 || ey >= c) { return false; } Player start; start.x = sx; start.y = sy; start.path = ""; memset(vis_player, 0, sizeof(vis_player)); vis_player[sx][sy] = 1; queue<Player>Q; Q.push(start); while(!Q.empty()) { Player hd = Q.front(); Q.pop(); if(hd.x == ex && hd.y == ey) { temp_ans = hd.path; return true; } for(int i = 0; i < 4; ++ i) { int x = hd.x + dir[i][0]; int y = hd.y + dir[i][1]; if(x == tx && y == ty) { continue; } if(ok(x, y) && !vis_player[x][y]) { if(map[x][y] != '#') { vis_player[x][y] = 1; Player t; t.x = x; t.y = y; t.path = hd.path + player_path[i]; Q.push(t); } } } } return false;}void Box_BFS() { memset(vis_box, 0, sizeof(vis_box)); Box start; start.x = bx; start.y = by; start.px = px; start.py = py; start.path = ""; vis_box[bx][by] = 1; queue<Box>Q; Q.push(start); while(!Q.empty()) { Box hd = Q.front(); Q.pop(); for(int i = 0; i < 4; ++ i) { int x = hd.x + dir[i][0]; int y = hd.y + dir[i][1]; if(ok(x, y) && !vis_box[x][y]) { if(map[x][y] != '#') { if(Player_BFS(hd.px, hd.py, hd.x - dir[i][0], hd.y - dir[i][1], hd.x, hd.y)) { Box t; t.x = x; t.y = y; t.px = hd.x; t.py = hd.y; vis_box[x][y] = 1; t.path = hd.path + temp_ans + box_path[i]; if(map[x][y] == 'T') { cout << t.path << endl; return; } else { Q.push(t); } } } } } } printf("Impossible.\n");}int main() { freopen("poj1475.txt", "r", stdin); int cnt = 0; while(scanf("%d%d", &r, &c), r) { for(int i = 0; i < r; ++ i) { scanf("%s", map[i]); for(int j = 0; j < c; ++ j) { if(map[i][j] == 'S') { px = i; py = j; } if(map[i][j] == 'B') { bx = i; by = j; } } } printf("Maze #%d\n", ++ cnt); Box_BFS(); printf("\n"); } return 0;}
- poj1475
- poj1475
- poj1475 Pushing Boxes
- poj1475 Pushing Boxes
- poj1475 推箱子
- POJ1475 Pushing Boxes(BFS+BFS)
- 【POJ1475】Pushing Boxes 推箱子
- 【POJ1475】Pushing Boxes-A*(或BFS?)
- #POJ1475#Pushing Boxes(A*_搜索)
- Flex 中有由于控件的属性includeInLayout导致滚动条不能出现!
- 外企招聘
- linux操作系统架构
- malloc & free
- VMware Tools安装
- poj1475
- 邻接表表示图
- 指针、结构体、联合体的安全规范
- C语言编译全过程
- php 文件处理
- 《数据挖掘概念与技术》读书笔记(一)
- Hibernate与iBATIS的比较
- 8086的存储器分段结构
- opengl