hdu 1885 Key Task

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1885 

Key Task

Description

The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places. 

The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game. 

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color. 

You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.

Input

The input consists of several maps. Each map begins with a line containing two integer numbers $R$ and $C$ $(1 leq R, C leq 100)$ specifying the map size. Then there are R lines each containing C characters. Each character is one of the following: 



Note that it is allowed to have 

  • more than one exit,
  • no exit at all,
  • more doors and/or keys of the same color, and
  • keys without corresponding doors and vice versa.



You may assume that the marker of your position (“*”) will appear exactly once in every map. 

There is one blank line after each map. The input is terminated by two zeros in place of the map size.

Output

For each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead. 

One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.

Sample Input

1 10
*........X

1 3
*#X

3 20
####################
#XY.gBr.*.Rb.G.GG.y#
####################

0 0

Sample Output

Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps.

状压bfs,每个格子16种状态,用三维数组标记即可。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::queue;
17 using std::vector;
18 using std::multimap;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 102;
29 typedef unsigned long long ull;
30 char G[N][N];
31 bool vis[N][N][18];
32 int H, W, Sx, Sy;
33 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
34 struct Node {
35     int x, y, key, s;
36     Node() {}
37     Node(int i, int j, int k, int l) :x(i), y(j), key(k), s(l) {}
38 };
39 inline int work(char ch) {
40     if (ch == 'B' || ch == 'b') return 0;
41     if (ch == 'Y' || ch == 'y') return 1;
42     if (ch == 'R' || ch == 'r') return 2;
43     if (ch == 'G' || ch == 'g') return 3;
44     return 0;
45 }
46 void bfs() {
47     bool f = true;
48     cls(vis, false);
49     queue<Node> q;
50     q.push(Node(Sx, Sy, 0, 0));
51     vis[Sx][Sy][0] = true;
52     while (!q.empty()) {
53         Node t = q.front(); q.pop();
54         if (G[t.x][t.y] == 'X') {
55             printf("Escape possible in %d steps.
", t.s);
56             return;
57         }
58         rep(i, 4) {
59             int key = t.key, x = t.x + dx[i], y = t.y + dy[i];
60             if (x < 0 || x >= H || y < 0 || y >= W) continue;
61             if (vis[x][y][key] || G[x][y] == '#') continue;
62             if (isupper(G[x][y]) && G[x][y] != 'X' && !(key &(1 << work(G[x][y])))) continue;
63             if (islower(G[x][y])) key |= 1 << work(G[x][y]);
64             q.push(Node(x, y, key, t.s + 1));
65             vis[x][y][key] = true;
66         }
67     }
68     puts("The poor student is trapped!");
69 }
70 int main() {
71 #ifdef LOCAL
72     freopen("in.txt", "r", stdin);
73     freopen("out.txt", "w+", stdout);
74 #endif
75     while (~scanf("%d %d", &H, &W), H + W) {
76         rep(i, H) {
77             scanf("%s", G[i]);
78             rep(j, W) {
79                 if (G[i][j] == '*') Sx = i, Sy = j;
80             }
81         }
82         bfs();
83     }
84     return 0;
85 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4638656.html