Problem Description
Zjt and Sara will take part in a game, named Game III. zjt and Sara will be in a maze, and Zjt must find Sara. there are some strang rules in this maze. if Zjt move a step, Sara will move a step in opposite direction.
Now give you the map, you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent.
Zjt can only move to a empty position int four diraction (up, left, right, down ). at the same time, Sara will move to a position in opposite direction, if there is empty. otherwise, she will not move to any position.
The map is a N * M two-dimen=array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.
>.: Empty position
> X: the wall
> Z: the position Zjt now stay
> S: the position Sara now stay
Your task is to find out the minimum steps they meet each other.
Input
The input contains several test cases. each test case starts with a line contains three number N, M (2 <= N <= 20, 2 <= M <= 20) indicate the size of the map. then N lines follows, each line contains M character. a z and a S will be in the map as the discription above.
Output
For each test case, you shocould print the minimum steps. "Bad Luck !" Will be print, if they can't meet each other.
Sample Input
4
XXXX
. Z ..
. XS.
XXXX
4
XXXX
. Z ..
. X. S
XXXX
4
XXXX
. ZX.
. XS.
XXXX
Sample Output
1
1
Bad Luck!
S and Z are in the same maze. One of them takes one step, and the other takes one step in the opposite direction. How many steps can be taken to the same location or the two are adjacent?
Idea: open a four-dimensional array record. Note that the initial position of the two persons must be set to "." Open Space.
Originally, I opened two 3D arrays at the beginning. Although I have passed the sample, I still cannot use A. Could you tell me which expert can use 3D for guidance?
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace std;
Int m, n;
Int z1, z2, s1, s2;
Char map [25] [25];
Int vis [25] [25] [25] [25];
Int to [4] [2] = {,-, 0,-, 1}; // Z route
Int to2 [4] [2] = {-, 0,-1}; // take the opposite route
Struct node
{
Int x1, x2, y1, y2, step;
};
Int check (int x, int y)
{
If (x <0 | y <0 | x> = n | y> = m | map [x] [y] = 'X ')
Return 1;
Return 0;
}
Int judge (int x, int x1, int y, int y1) // conditions that meet the conditions
{
If (x = x1 & y = y1)
Return 1;
If (x = x1 + 1 & y = y1)
Return 1;
If (x = x1-1 & y = y1)
Return 1;
If (x = x1 & y = y1-1)
Return 1;
If (x = x1 & y = y1 + 1)
Return 1;
Return 0;
}
Int bfs ()
{
Int I;
Queue <node> Q;
Node a, next;
Memset (vis, 0, sizeof (vis ));
A. x1 = z1;
A. x2 = s1;
A. y1 = z2;
A. y2 = s2;
A. step = 0;
Vis [z1] [z2] [s1] [s2] = 1; // a four-dimensional Array records the walking condition.
Q. push ();
While (! Q. empty ())
{
A = Q. front ();
Q. pop ();
If (judge (a. x1, a. x2, a. y1, a. y2 ))
Return a. step;
For (I = 0; I <4; I ++)
{
Next =;
Next. x1 = a. x1 + to [I] [0];
Next. y1 = a. y1 + to [I] [1];
Next. x2 = a. x2 + to2 [I] [0];
Next. y2 = a. y2 + to2 [I] [1];
If (check (next. x1, next. y1 ))
Continue;
If (check (next. x2, next. y2 ))
{
Next. x2 = a. x2;
Next. y2 = a. y2;
}
If (vis [next. x1] [next. y1] [next. x2] [next. y2])
Continue;
Vis [next. x1] [next. y1] [next. x2] [next. y2] = 1;
Next. step = a. step + 1;
Q. push (next );
}
}
Return 0;
}
Int main ()
{
While (~ Scanf ("% d", & n, & m ))
{
Int I, j;
For (I = 0; I <n; I ++)
{
Scanf ("% s", map [I]);
For (j = 0; map [I] [j]; j ++)
{
If (map [I] [j] = 'Z ')
{
Map [I] [j] = '.'; // set the initial position to open space.
Z1 = I;
Z2 = j;
}
Else if (map [I] [j] ='s ')
{
Map [I] [j] = '.';
S1 = I;
S2 = j;
}
}
}
Int ans;
Ans = bfs ();
If (ans)
Printf ("% d \ n", ans );
Else
Printf ("Bad Luck! \ N ");
}
Return 0;
}
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace std;
Int m, n;
Int z1, z2, s1, s2;
Char map [25] [25];
Int vis [25] [25] [25] [25];
Int to [4] [2] = {,-, 0,-, 1}; // Z route
Int to2 [4] [2] = {-, 0,-1}; // take the opposite route
Struct node
{
Int x1, x2, y1, y2, step;
};
Int check (int x, int y)
{
If (x <0 | y <0 | x> = n | y> = m | map [x] [y] = 'X ')
Return 1;
Return 0;
}
Int judge (int x, int x1, int y, int y1) // conditions that meet the conditions
{
If (x = x1 & y = y1)
Return 1;
If (x = x1 + 1 & y = y1)
Return 1;
If (x = x1-1 & y = y1)
Return 1;
If (x = x1 & y = y1-1)
Return 1;
If (x = x1 & y = y1 + 1)
Return 1;
Return 0;
}
Int bfs ()
{
Int I;
Queue <node> Q;
Node a, next;
Memset (vis, 0, sizeof (vis ));
A. x1 = z1;
A. x2 = s1;
A. y1 = z2;
A. y2 = s2;
A. step = 0;
Vis [z1] [z2] [s1] [s2] = 1; // a four-dimensional Array records the walking condition.
Q. push ();
While (! Q. empty ())
{
A = Q. front ();
Q. pop ();
If (judge (a. x1, a. x2, a. y1, a. y2 ))
Return a. step;
For (I = 0; I <4; I ++)
{
Next =;
Next. x1 = a. x1 + to [I] [0];
Next. y1 = a. y1 + to [I] [1];
Next. x2 = a. x2 + to2 [I] [0];
Next. y2 = a. y2 + to2 [I] [1];
If (check (next. x1, next. y1 ))
Continue;
If (check (next. x2, next. y2 ))
{
Next. x2 = a. x2;
Next. y2 = a. y2;
}
If (vis [next. x1] [next. y1] [next. x2] [next. y2])
Continue;
Vis [next. x1] [next. y1] [next. x2] [next. y2] = 1;
Next. step = a. step + 1;
Q. push (next );
}
}
Return 0;
}
Int main ()
{
While (~ Scanf ("% d", & n, & m ))
{
Int I, j;
For (I = 0; I <n; I ++)
{
Scanf ("% s", map [I]);
For (j = 0; map [I] [j]; j ++)
{
If (map [I] [j] = 'Z ')
{
Map [I] [j] = '.'; // set the initial position to open space.
Z1 = I;
Z2 = j;
}
Else if (map [I] [j] ='s ')
{
Map [I] [j] = '.';
S1 = I;
S2 = j;
}
}
}
Int ans;
Ans = bfs ();
If (ans)
Printf ("% d \ n", ans );
Else
Printf ("Bad Luck! \ N ");
}
Return 0;
}