Http://poj.org/problem? Id = 2935
Basic wall maze
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:2794 |
|
Accepted:1271 |
|
Special Judge |
Description In this problem you have to solve a very simple maze consisting:
- A 6 by 6 grid of unit squares
- 3 wallof length between 1 and 6 which are placed either horizontally or vertically to separate squares
- One start and one end marker
A maze may look like this: You have to find a shortest path between the square with the start marker and the square with the End marker. only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. it is not allowed to leave the grid. Input The input consists of several test cases. each test case consists of five lines: the first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the End marker. the third, fourth and th lines specify the locations of the three Wils. the location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end point followed by the position of its lower end point (in case of a vertical wall ). the position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid. You may assume that the three walldon't intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. moreover, there will always be a valid path from the start marker to the End marker. note that the sample input specifies the maze from the picture above. The last test case is followed by a line containing two zeros. Output For each test case print a description of a shortest path from the start marker to the End marker. the description shoshould specify the direction of every move ('n' for up, 'E' for right,'s for down and 'W' for left ). There can be more than one shortest path, in this case you can print any of them. Sample Input 1 62 60 0 1 01 5 1 61 5 3 50 0 Sample output NEEESWW Source Ulm Local 2006 |
[Submit] [Go Back] [Status] [discuss]
Solution: simple BFs, the key lies in the processing of the Wall, using a three-dimensional array, map [8] [8] [4] The first two dimensions represent coordinates, the last one represents four directions, the orientation of the wall is set to 1, and the orientation of the wall is set to 0.
Solution code:
1 # include <stdio. h> 2 # include <string. h> 3 # include <queue> 4 # include <string> 5 # include <iostream> 6 using namespace STD; 7 8 int map [8] [8] [4]; // indicates the wall // 1 9 bool vis [8] [8]; // indicates whether 10 const int turn_x [4] = {0, 1, 0, -1}; // 2 11 const int turn_y [4] = {-1, 0, 1, 0 }; // 212 const Char to [4] = {'n', 'E', 's', 'w'}; // 313/* 1, 2, 3 synchronize the last coordinate */14 struct node {15 int X, Y; 16 string pass; 17}; 18 in T sta_x, sta_y, end_x, end_y; 19 queue <node> q; 20 node P1, P2; 21 22 void BFS () {23 int XX, YY; 24 while (! Q. empty () 25 Q. pop (); 26 memset (VIS, 0, sizeof (VIS); 27 vis [sta_x] [sta_y] = 1; 28 p1.x = sta_x; 29 p1.y = sta_y; 30 p1.pass = ""; 31 Q. push (P1); 32 While (! Q. empty () {33 p1 = Q. front (); 34 Q. pop (); 35 for (INT I = 0; I <4; I ++) {// synchronize coordinates 36 xx = p1.x + turn_x [I] here; 37 YY = p1.y + turn_y [I]; 38 If (XX <= 0 | XX> 6 | YY <= 0 | YY> 6) 39 continue; 40 if (Map [p1.x] [p1.y] [I] | Vis [XX] [YY]) 41 continue; 42 vis [XX] [YY] = 1; 43 p2.x = xx; 44 p2.y = YY; 45 p2.pass = p1.pass + to [I]; 46 If (p2.x = end_x & p2.y = end_y) 47 return; 48 Q. push (P2); 49} 50} 51} 52 53 int Mai N () {54 int xx1, YY1, xx2, yy2, TM; 55 while (~ Scanf ("% d", & sta_x, & sta_y) & sta_x & sta_y) {56 scanf ("% d", & end_x, & end_y ); 57 memset (MAP, 0, sizeof (MAP); 58 59 for (INT I = 0; I <3; I ++) {60 scanf ("% d", & xx1, & YY1, & xx2, & yy2); 61 If (xx1 = xx2) {62 if (YY1> yy2) {63 TM = YY1; 64 YY1 = yy2; 65 yy2 = TM; 66} 67 for (Int J = YY1 + 1; j <= yy2; j ++) {68 map [xx1 + 1] [J] [3] = 1; 69 map [xx1] [J] [1] = 1; 70} 71} 72 else {73 If (xx1> xx2) {74 TM = xx1; 75 xx1 = xx2; 76 xx2 = TM; 77} 78 for (Int J = xx1 + 1; j <= xx2; j ++) {79 map [J] [YY1] [2] = 1; 80 map [J] [YY1 + 1] [0] = 1; 81} 82} 83} 84 BFS (); 85 cout <p2.pass <Endl; 86} 87 return 0; 88}
View code