Recently DZB is playing a game called Dot Dot Dot. It's a simple game but very interesting. The rule is like this:
The game is played on a N*M grid map, each grid is adjacent with 4 grids(up, down, left, right). There's only one exit on the map, the game finished when the player reach the exit. When the game starts, the player go to the exit step by step. In each step he can only move to adjacent grid. But there's some barriers on the map, each barrier is yellow or green. The player can not go to the grid when there is a barrier or there is outside wall.
The only way to destroy the barriers is to stand on some grid which has a trigger. Each trigger is also yellow or green and can be used only once. When the player stand on it, it will sent shock waves to destroy all the barriers with the same color it could reach directly. Shock waves spread as wide as possible. It can spread on all the connected grids which no barrier is on it, that means if it reach a barrier with different color, it will be blocked too. Notice if the wave can not reach a barrier directly, it can not be destroied, even if it's the same color with the trigger.
Now DZB not only wants to finish the game, but also wants to finish the game with the minimal step, so he ask you for help.
Input
There are multiple test cases. For each test case:
The first line contains two integers N(1≤N≤40), M(1≤M≤40), N is the number of rows of the map, and M is the number of the columns.
The after N lines describe the map, each line has M characters. A character could be:
- '.' means there's nothing, you can stand there.
- '*' means the outside wall, we guarantee that the outside wall form a close area and the player is in it.
- 'Y' means a yellow barrier, you can not stand there unless you have destroied the barrier.
- 'G' means a green barrier, similar with 'Y'.
- 'y' means a yellow trigger.
- 'g' means a green trigger. Notice the number of yellow trigger plus the number of green trigger is no more than 5.
- 's' means the initial position of the player.
- 'e' means the exit, the player must go to there finally to finish the game.
There is a blank line between every two cases.
Process to the end of input.
Output
One line for each test case. The minimal step. If can not reach the exit, print -1 instead.
Sample Input
5 17 ***************** *.......Y.......* *.y.s...Y....e..* *.......Y.......* *****************
Sample Output
13
解题思路:由于只有5个trigger,以不同的顺序到达trigger,每一种顺序可以用一个状态表示。求所有状态下的最短路就可以了。
/******************************** * 作者:Linfly * 时间:2014-07-16 21:11 ********************************/ #include <iostream> #include <vector> #include <map> #include <queue> #include <set> #include <algorithm> #include <stack> #include <cstring> using namespace std; #define MAXN 50 struct stNode{ int x,y; int f; stNode(int xx, int yy, int ff) { x = xx, y = yy, f =ff; } stNode() { x = y = f = 0; } }; char m[MAXN][MAXN], g[MAXN][MAXN]; int N,M; int mindist; int sx,sy,ex,ey; int trigger; int seq[6]; int DX[4] = { 0,-1,0,1}; int DY[4] = {-1, 0,1,0}; void GetXY(int no, int &x, int &y) { int cnt = 0; for (int i = 0; i < N; i++) { for (int j = 0; j<M; j++) { if (m[i][j] == 'y' || m[i][j] == 'g') cnt ++; if (cnt == no) { x = i; y = j; return ; } } } } int BFS(int curx, int cury, int dx, int dy, int &dd) { int dist = 0xfffffff; int mdist[MAXN][MAXN]; memset(mdist, 0x7f, sizeof(mdist)); bool inQueue[MAXN][MAXN]; memset(inQueue, false, sizeof(inQueue)); queue<struct stNode> Q; Q.push(stNode(curx, cury, 0)); inQueue[curx][cury] = true; mdist[curx][cury] = 0; while (!Q.empty()) { stNode node = Q.front(); Q.pop(); for (int i = 0; i<4; i++) { int xx = node.x + DX[i]; int yy = node.y + DY[i]; if (xx == dx && yy == dy && dist > node.f+1) //到触发器的最短路径 { dist = node.f + 1; } if (xx == ex && yy == ey && dd > node.f+1) //当前状态到终点的路径 { dd = node.f + 1; } if (!inQueue[xx][yy] && node.f+1<mdist[xx][yy] && (g[xx][yy] == 's' || g[xx][yy] == '.' || g[xx][yy] == 'y' || g[xx][yy] == 'g')) { Q.push(stNode(xx, yy, node.f+1)); inQueue[xx][yy] = true; mdist[xx][yy] = node.f+1; } if (g[dx][dy] - g[xx][yy] == 32) //重建图 { g[xx][yy] = '#'; } } inQueue[node.x][node.y] = false; } /* cout << "++++++++++++++++++++++++++++++++++++++++" << endl; cout << "curx:" << curx << " cury:" << cury << " dx:" << dx << " dy:" << dy<< endl; cout << "dist:" << dist << " dd:" << dd << endl; for (int i = 0; i < N; i++) cout << g[i] << endl; cout << "++++++++++++++++++++++++++++++++++++++++" << endl; */ return dist; } bool Check(int seq[]) { int curx = sx, cury = sy; int dx, dy; int sumdist = 0; //BFS(curx, cury, ex, ey, sumdist); int i = 0; for (i = 0; i<trigger; i++) { dx = dy = 0; GetXY(seq[i],dx,dy); //得到触发器坐标 int dd = 0xfffffff; int dist = BFS(curx, cury, dx, dy, dd); if (dd != 0xfffffff && sumdist+dd < mindist) { mindist = sumdist + dd; } if (dist == 0xfffffff) //无法到达状态(dx,dy) break; sumdist += dist; curx = dx; cury = dy; for (int k = 0; k<N; k++) { for (int j = 0; j<M; j++) { if (g[k][j] == '#') g[k][j] = '.'; } } } if (i == trigger) { int dd = 0xfffffff; BFS(curx, cury, ex, ey, dd); if (dd != 0xfffffff && sumdist+dd < mindist) { mindist = sumdist + dd; } } return true; } int main() { while (cin >> N >> M) { memset(m , 0, sizeof(m)); trigger = 0; mindist = 0xfffffff; for (int i = 0; i < N; i++) { cin >> m[i]; for (int j = 0; j < M; j++) { if (m[i][j] == 'y' || m[i][j] == 'g') trigger++; else if (m[i][j] == 's') { sx = i; sy = j; } else if (m[i][j] == 'e') { ex = i; ey = j; } } } /* for (int i = 0; i < N; i++) cout << m[i] << endl; */ for (int i = 0; i<trigger; i++) //编号触发点 seq[i] = i+1; do{ /* for (int i = 0; i < trigger; i++) cout << seq[i] << " "; cout << endl; */ memcpy(g, m, sizeof(m)); /* for (int i = 0; i < N; i++) cout << g[i] << endl; */ Check(seq); }while (next_permutation(seq, seq+trigger)); if (mindist == 0xfffffff) cout << -1 << endl; else cout << mindist << endl; } return 0; }