Angel was caught bythe MOLIGPY! He was putin prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs inthe prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that"approach Angel"istogettothe position where Angel stays. When there's a guard inthe grid, we must kill him (or her?) to move intothe grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal timeto approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, tothe neighbor grid within bound, of course.)
Input
1 2 3 4 5
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and"r" stands for each of Angel's friend.
Processto the endof the file.
Output
1
For each test case, your program should output a single integer, standing forthe minimal time needed. If such anumber does no exist, you should output aline containing "Poor ANGEL has to stay in the prison all his life."
voiddfs(int x, int y, int len) { if (x < 0 || x >= n || y < 0 || y >= m || mark[x][y] == 1) return;//边界判定 if (len >= minl || c[x][y] == '#') return;//边界判定2 if (c[x][y] == 'r')//到达终点 { if (len < minl) minl = len; return; } if (c[x][y] == 'x') len++;//杀守卫 mark[x][y] = 1; dfs(x + 1, y, len + 1);//继续dfs dfs(x - 1, y, len + 1); dfs(x, y + 1, len + 1); dfs(x, y - 1, len + 1); mark[x][y] = 0;//还原 }
intmain() { while (cin >> n >> m) { memset(mark, 0, sizeof(mark)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> c[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (c[i][j] == 'a') { startx = i; starty = j; } len = 0; minl = 99999; dfs(startx, starty, len); if (minl == 10) minl++; if (minl < 99999) cout << minl << endl; elsecout << "Poor ANGEL has to stay in the prison all his life." << endl; } return0; }