Rescue LK
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 83 Accepted: 42
Description
Ziliang loves LK. But LK was kidnapped by Monster. oyy, and was put in a labyrinth.
After thousands of hard fighting, ziliang finally enter the labyrinth. He must find a way to rescue LK !!! He has a map of the labyrinth, but the puzzle is too complicated, that he need your help.
Input
Input has several case. each case first give an n and m, that is the height and length of the labyrinth. followed by the map. in the map, '# 'means the wall, 'z' means the position of ziliang, 'l'
Means position of LK. The '.' Means empty cells.
Ziliang can only walk on empty cell, and can only go up, down, left, right. And n, m will be less than 50.
Output
If ziliang can reach LK, output "LK has a good life"
Else output "Mission Failed"
Sample Input
5 5######Z..####.##..L######7 7########Z....######.######.##..#..##L.###########
Sample Output
LK has a good lifeMission Failed
Source
GDUT Monthly 2007.11 by ziliang
/* Typical Maze problem of compaction: the common practice is stack and DFS. However, in the final analysis, it is also a method of backtracking, the difference between stack and DFS is recursion and non-recursion. General idea: Use DFS for path exploration. Each step of exploration is feasible. Set the flag to go down until you find the end point or encounter a grid that is not feasible. If the end point is found, 1 is returned. If the result is not feasible, the last layer is returned, and the grid that has been crossed is retained. Status: AC, 0 MS running */# include <stdio. h> char chMap [52] [52]; const int nDir [4] [2] ={{}, {}, {-}, {0, -1 }}; // int m = 0; int n = 0; int zX = 0; // The X coordinate of the start point int zY = 0; // Y coordinate of the start point int DFS (int x, int y); int main (void) {int I = 0; int j = 0; while (scanf ("% d", & n, & m )! = EOF) {getchar (); for (I = 0; I <n; ++ I) {for (j = 0; j <m; ++ j) {scanf ("% c", & chMap [I] [j]); if ('Z' = chMap [I] [j]) {zX = I; zY = j ;}} getchar () ;}if (1 = DFS (zX, zY) {puts ("LK has a good life ");} else {puts ("Mission Failed") ;}} return 0 ;}int DFS (int x, int y) {int I = 0; int j = 0; if (x <0 | y <0 | x> = n | y> = m) {return 0;} for (I = 0; I <4; ++ I) // change the direction {if ('l' = chMap [x + nDir [I] [0] [y + nDir [I] [1]) {return 1;} else if ('. '= chMap [x + nDir [I] [0] [y + nDir [I] [1]) {chMap [x + nDir [I] [0] [y + nDir [I] [1] = '#'; // If the grid is feasible, set if (1 = DFS (x + nDir [I] [0], y + nDir [I] [1]) // set this direction to continue {return 1;} chMap [x + nDir [I] [0] [y + nDir [I] [1] = '. '; // reset the original status .}} Return 0 ;}