Holedox Moving
Time Limit: 5000 MS Memory Limit: 65536 K
Total Submissions: 11975 Accepted: 2873
Description
During winter, the most hungry and severe time, Holedox sleeps in its lair. when spring comes, Holedox wakes up, moves to the exit of its lair, comes out, and begins its new life.
Holedox is a special snake, but its body is not very long. its lair is like a maze and can be imagined as a rectangle with n * m squares. each square is either a stone or a vacant place, and only vacant places allow Holedox to move in. using ordered pair of row and column number of the lair, the square of exit located at (1, 1 ).
Holedox's body, whose length is L, can be represented block by block. and let B1 (r1, c1) B2 (r2, c2 ).. BL (rL, cL) denote its L length body, where Bi is adjacent to Bi + 1 in the lair for 1 <= I <= L-1, and B1 is its head, BL is its tail.
To move in the lair, Holedox chooses an adjacent vacant square of its head, which is neither a stone nor occupied by its body. then it moves the head into the vacant square, and at the same time, each other block of its body is moved into the square occupied by the corresponding previous block.
For example, in the Figure 2, at the beginning the body of Holedox can be represented as B1 () B2 () B3 () B4 ). during the next step, observing that B1 '(5, 1) is the only square that the head can be moved into, Holedox moves its head into B1' (5, 1 ), then moves B2 into B1, B3 into B2, and B4 into B3. Thus after one step, the body of Holedox locates in B1 (5, 1) B2 (4, 1) B3 (4, 2) b4 (3, 2) (see the Figure 3 ).
Given the map of the lair and the original location of each block of Holedox's body, your task is to write a program to tell the minimal number of steps that Holedox has to take to move its head to reach the square of exit (1, 1 ).
Input
The input consists of several test cases. the first line of each case contains three integers n, m (1 <= n, m <= 20) and L (2 <= L <= 8 ), representing the number of rows in the lair, the number of columns in the lair and the body length of Holedox, respectively. the next L lines contain a pair of row and column number each, indicating the original position of each block of Holedox's body, from B1 (r1, c1) to BL (rL, cL) orderly, where 1 <= ri <= n, and 1 <= ci <= m, 1 <= I <= L. the next line contains an integer K, representing the number of squares of stones in the lair. the following K lines contain a pair of row and column number each, indicating the location of each square of stone. then a blank line follows to separate the cases.
The input is terminated by a line with three zeros.
Note: Bi is always adjacent to Bi + 1 (1 <= I <= L-1) and exit square () will never be a stone.
Output
For each test case output one line containing the test case number followed by the minimal number of steps Holedox has to take. "-1" means no solution for that case.
Sample Input
5 6 4
4 1
4 2
3 2
3 1
3
2 3
3 3
3 4
4 4 4
2 3
1 3
1 4
2 4
4
2 1
2 2
3 4
4 2
0 0 0
This is a question of BFS, but because snakes can be moved in this question, a simple two-dimensional mark array cannot be used to determine the weight. For example:
If an array is marked with a simple two-dimensional array, this example is unsolvable. Obviously, there is a minimum number of moves for this graph.
Then I want to mark it with a three-dimensional array, that is, adding a direction based on the original two-dimensional array. If the snake header passes through the current grid and comes from another direction, it enters the queue. However, after doing so, we found that there are still bugs, but the previous example still won't work.
Then I tried to use the snake's head and tail to judge the weight (a four-dimensional array). I felt okay and the test data could pass through, but it was still wrong. I thought it was still a problem.
At last, I had to record the status of the entire snake, but I really don't know how to compress it. After reading the report, you can remember to record the position of each node relative to the previous node. In this way, you can use a three-dimensional array to determine the location, and the memory will not burst.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <cmath>
# Include <cstring>
Using namespace std;
Struct st
{
Int step;
Int B [10] [2]; // The Position of the snake
} W, v;
Int dir [4] [2] = {0, 1}, {0,-1 }};
Int map [21] [21]; // 0 indicates the open space, and 1 indicates the position of the stone.
Bool flag [21] [21] [16384]; // use a 4-digit compression state, with a maximum value of 4 ^ 7-1
Int n, m, L, K;
Int location (int px, int py, int nx, int ny) // calculates the relative position of the two nodes.
{
If (px = nx)
{
If (py> ny)
Return 0;
Else
Return 1;
}
Else
{
If (px> nx)
Return 2;
Else
Return 3;
}
}
Int getstate (int B [] [2]) // compression status
{
Int s = 0;
For (int I = 0; I <L-1; I ++)
S = s * 4 + location (B [I] [0], B [I] [1], B [I + 1] [0], B [I + 1] [1]);
Return s;
}
Bool path (int x, int y, int B [] [2])
{
If (x <1 | x> n | y <1 | y> m | map [x] [y]! = 0)
Return false;
For (int I = 0; I <L; I ++)
If (x = B [I] [0] & y = B [I] [1])
Return false;
Return true;
}
Int bfs ()
{
If (w. B [0] [0] = 1 & w. B [0] [1] = 1)
Return 0;
Queue <st> q;
Q. push (w );
While (! Q. empty ())
{
W = q. front ();
Q. pop ();
For (int I = 0; I <4; I ++)
{
Int x, y, state;
X = w. B [0] [0] + dir [I] [0];
Y = w. B [0] [1] + dir [I] [1];
If (x = 1 & y = 1)
Return w. step + 1;
If (! Path (x, y, w. B ))
Continue;
V. step = w. step + 1;
For (int j = L-1; j> 0; j --)
{
V. B [j] [0] = w. B [J-1] [0];
V. B [j] [1] = w. B [J-1] [1];
}
V. B [0] [0] = x;
V. B [0] [1] = y;
State = getstate (v. B );
If (flag [x] [y] [state])
Continue;
Flag [x] [y] [state] = true;
Q. push (v );
}
}
Return-1;
}
Int main ()
{
Int Case = 1;
While (scanf ("% d", & n, & m, & L), n + m + L)
{
Memset (map, 0, sizeof (map ));
Int mst = pow (4, L-1 );
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
For (int k = 0; k <mst; k ++)
Flag [I] [j] [k] = false;
For (int I = 0; I <L; I ++)
{
Scanf ("% d", & w. B [I] [0], & w. B [I] [1]);
}
Scanf ("% d", & K );
For (int I = 0; I <K; I ++)
{
Int row, col;
Scanf ("% d", & row, & col );
Map [row] [col] = 1;
}
W. step = 0;
Printf ("Case % d: % d \ n", Case ++, bfs ());
}
Return 0;
}
/*
4 4 4
3 2
3 1
2 1
2 2
6
1 2
1 3
1 4
2 3
2 4
4 1
*/
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <cmath>
# Include <cstring>
Using namespace std;
Struct st
{
Int step;
Int B [10] [2]; // The Position of the snake
} W, v;
Int dir [4] [2] = {0, 1}, {0,-1 }};
Int map [21] [21]; // 0 indicates the open space, and 1 indicates the position of the stone.
Bool flag [21] [21] [16384]; // use a 4-digit compression state, with a maximum value of 4 ^ 7-1
Int n, m, L, K;
Int location (int px, int py, int nx, int ny) // calculates the relative position of the two nodes.
{
If (px = nx)
{
If (py> ny)
Return 0;
Else
Return 1;
}
Else
{
If (px> nx)
Return 2;
Else
Return 3;
}
}
Int getstate (int B [] [2]) // compression status
{
Int s = 0;
For (int I = 0; I <L-1; I ++)
S = s * 4 + location (B [I] [0], B [I] [1], B [I + 1] [0], B [I + 1] [1]);
Return s;
}
Bool path (int x, int y, int B [] [2])
{
If (x <1 | x> n | y <1 | y> m | map [x] [y]! = 0)
Return false;
For (int I = 0; I <L; I ++)
If (x = B [I] [0] & y = B [I] [1])
Return false;
Return true;
}
Int bfs ()
{
If (w. B [0] [0] = 1 & w. B [0] [1] = 1)
Return 0;
Queue <st> q;
Q. push (w );
While (! Q. empty ())
{
W = q. front ();
Q. pop ();
For (int I = 0; I <4; I ++)
{
Int x, y, state;
X = w. B [0] [0] + dir [I] [0];
Y = w. B [0] [1] + dir [I] [1];
If (x = 1 & y = 1)
Return w. step + 1;
If (! Path (x, y, w. B ))
Continue;
V. step = w. step + 1;
For (int j = L-1; j> 0; j --)
{
V. B [j] [0] = w. B [J-1] [0];
V. B [j] [1] = w. B [J-1] [1];
}
V. B [0] [0] = x;
V. B [0] [1] = y;
State = getstate (v. B );
If (flag [x] [y] [state])
Continue;
Flag [x] [y] [state] = true;
Q. push (v );
}
}
Return-1;
}
Int main ()
{
Int Case = 1;
While (scanf ("% d", & n, & m, & L), n + m + L)
{
Memset (map, 0, sizeof (map ));
Int mst = pow (4, L-1 );
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
For (int k = 0; k <mst; k ++)
Flag [I] [j] [k] = false;
For (int I = 0; I <L; I ++)
{
Scanf ("% d", & w. B [I] [0], & w. B [I] [1]);
}
Scanf ("% d", & K );
For (int I = 0; I <K; I ++)
{
Int row, col;
Scanf ("% d", & row, & col );
Map [row] [col] = 1;
}
W. step = 0;
Printf ("Case % d: % d \ n", Case ++, bfs ());
}
Return 0;
}
/*
4 4 4
3 2
3 1
2 1
2 2
6
1 2
1 3
1 4
2 3
2 4
4 1
*/