poj1475 Pushing Boxes(BFS)

题目链接

http://poj.org/problem?id=1475

题意

推箱子游戏。输入迷宫、箱子的位置、人的位置、目标位置,求人是否能把箱子推到目标位置,若能则输出推的最少的路径,如果有多条步数相同的推的最少的路径,则输出总步数(人走的步数+推箱子的步数)最少的那条路径;若不能把箱子推到目标位置,则输出Impossible.

思路

先求出箱子到目标位置的最短路径(bfs_box),在bfs1推箱子的过程中,根据推的方向和箱子的位置得到人的位置,再求得人到达这个位置的最短路(bfs_person)即可。

代码

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <string>
  5 #include <queue>
  6 using namespace std;
  7 
  8 struct Node
  9 {
 10     int br, bc;        //box_row,box_col
 11     int pr, pc;        //person_row,person_col
 12     string ans;
 13 
 14     Node() {}
 15     Node(int br, int bc, int pr, int pc, string ans) :br(br), bc(bc), pr(pr), pc(pc), ans(ans) {}
 16 };
 17 
 18 const int N = 20;
 19 int m, n;
 20 char maze[N][N];
 21 int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
 22 char push[] = { 'N', 'S', 'W', 'E' };
 23 char walk[] = { 'n', 's', 'w', 'e' };
 24 int br, bc;
 25 int pr, pc;
 26 int tr, tc;
 27 
 28 bool ok(int r, int c)
 29 {
 30     if (r >= 0 && r < m && c >= 0 && c < n && maze[r][c] != '#')
 31         return true;
 32     return false;
 33 }
 34 
 35 string tmp;
 36 bool bfs_person(int sr, int sc, int er, int ec, Node node)
 37 {
 38     tmp = "";
 39     int visit[N][N];
 40     memset(visit, 0, sizeof(visit));
 41     queue<Node> q;
 42     q.push(Node(-1, -1, sr, sc, ""));
 43     visit[sr][sc] = 1;
 44     visit[node.br][node.bc] = 1;    //注意
 45     while (!q.empty())
 46     {
 47         Node cur = q.front();
 48         q.pop();
 49         if (cur.pr == er && cur.pc == ec)
 50         {
 51             tmp = cur.ans;
 52             return true;
 53         }
 54         for (int i = 0; i < 4; i++)
 55         {
 56             int nr = cur.pr + dir[i][0];
 57             int nc = cur.pc + dir[i][1];
 58             if (ok(nr, nc) && !visit[nr][nc])
 59             {
 60                 visit[nr][nc] = 1;
 61                 string ans = cur.ans + walk[i];
 62                 q.push(Node(-1, -1, nr, nc, ans));
 63             }
 64         }
 65     }
 66     return false;
 67 }
 68 
 69 string bfs_box()
 70 {
 71     int visit[N][N];
 72     memset(visit, 0, sizeof(visit));
 73     queue<Node> q;
 74     q.push(Node(br, bc, pr, pc, ""));
 75     visit[br][bc] = 1;
 76     while (!q.empty())
 77     {
 78         Node cur = q.front();
 79         q.pop();
 80         if (cur.br == tr && cur.bc == tc)
 81             return cur.ans;
 82         for (int i = 0; i < 4; i++)
 83         {
 84             int nr = cur.br + dir[i][0];
 85             int nc = cur.bc + dir[i][1];
 86             int pre_r = cur.br - dir[i][0];
 87             int pre_c = cur.bc - dir[i][1];
 88             if (ok(nr, nc) && ok(pre_r, pre_c) && !visit[nr][nc])
 89             {
 90                 if (bfs_person(cur.pr, cur.pc, pre_r, pre_c, cur))
 91                 {
 92                     visit[nr][nc] = 1;
 93                     Node next;
 94                     next.br = nr;
 95                     next.bc = nc;
 96                     next.pr = cur.br;
 97                     next.pc = cur.bc;
 98                     next.ans = cur.ans + tmp + push[i];
 99                     q.push(next);
100                 }
101             }
102         }
103     }
104     return "Impossible.";
105 }
106 
107 int main()
108 {
109     //freopen("poj1475.txt", "r", stdin);
110     int cnt = 0;
111     while (cin >> m >> n && m)
112     {
113         for (int i = 0; i < m; i++)
114         {
115             for (int j = 0; j < n; j++)
116             {
117                 cin >> maze[i][j];
118                 if (maze[i][j] == 'S')
119                 {
120                     pr = i;
121                     pc = j;
122                 }
123                 else if (maze[i][j] == 'B')
124                 {
125                     br = i;
126                     bc = j;
127                 }
128                 else if (maze[i][j] == 'T')
129                 {
130                     tr = i;
131                     tc = j;
132                 }
133             }
134         }
135         printf("Maze #%d\n", ++cnt);
136         cout << bfs_box() << endl << endl;
137     }
138     return 0;
139 }
posted @ 2017-11-20 20:30  ColdCode  阅读(351)  评论(1编辑  收藏  举报
AmazingCounters.com