POJ-1324-Holedox (BFS)

Source: Internet
Author: User

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 <= drawing 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 44 14 23 23 132 33 33 44 4 42 31 31 42 442 12 23 44 20 0 0

Sample output

Case 1: 9Case 2: -1

Hint

In the above sample case, the head of holedox can follows ()->) -> () to reach the square of exit with minimal number of step, which is nine.

Source

Beijing 2002


Idea: Start from the head and start to think about the tail. Different Directions represent different values, and the snake state is obtained. Then, it is a common BFs. You must also note that you cannot bite yourself or connect yourself to the head or tail.


# Include <stdio. h> struct s {int step, X [9], Y [9], head;} que [1000000]; bool vis [20] [20] [16384]; int MP [20] [20], NXT [4] [2] = {}, {0,-1 }}, mi [8] = {,}; int main () {int N, M, L, I, J, K, X, Y, Head, temp, lx, Ly, Tl, valx, Valy, casenum = 1, top, bottom; while (~ Scanf ("% d", & N, & M, & L) & N) {for (I = 0; I <L; I ++) {scanf ("% d", & X, & Y); que [0]. X [I] = X-1; que [0]. Y [I] = Y-1;} for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) MP [I] [J] = 1; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) for (k = 0; k <mi L-1]; k ++) vis [I] [J] [k] = 0; scanf ("% d ", & K); While (k --) {scanf ("% d", & X, & Y); MP [x-1] [Y-1] = 0 ;} que [0]. step = 0; que [0]. head = 0; Top = 0; bottom = 1; while (top <bottom) {If (! Que [Top]. X [que [Top]. Head] &! Que [Top]. Y [que [Top]. head]) {printf ("case % d: % d \ n", casenum ++, que [Top]. step); break;} head = que [Top]. head-1; if (Head =-1) Head = L; que [Top]. step ++; for (I = 0; I <4; I ++) {x = que [Top]. X [head] = que [Top]. X [que [Top]. head] + NXT [I] [0]; y = que [Top]. Y [head] = que [Top]. Y [que [Top]. head] + NXT [I] [1]; if (x> = 0 & x <n & Y> = 0 & Y <M & amp; MP [x] [Y]) {temp = 0; lx = x; Ly = y; TL = 0; For (j = que [Top]. head; TL <L; j ++) {If (j = L + 1) J = 0; If (Q UE [Top]. X [J] = x & que [Top]. Y [J] = y) break; // if it bit itself, if (TL L-1) // The computing status {valx = Lx-que [Top]. X [J]; Valy = Ly-que [Top]. Y [J]; If (valx = 0 & Valy = 1) temp = temp <2; else if (valx = 1 & Valy = 0) temp = temp <2 | 1; else if (valx =-1 & Valy = 0) temp = temp <2 | 2; else if (valx = 0 & Valy =-1) temp = temp <2 | 3; Lx = que [Top]. X [J]; Ly = que [Top]. Y [J];} TL ++;} If (TL = L &&! Vis [x] [y] [temp]) // if you do not bit yourself, and this status has not been accessed {vis [x] [y] [temp] = 1; que [Top]. head --; If (que [Top]. head =-1) que [Top]. head = L; que [Top]. X [head] = x; que [Top]. Y [head] = y; que [bottom ++] = que [Top]; que [Top]. head ++; If (que [Top]. head = L + 1) que [Top]. head = 0 ;}}top ++;} If (Top = bottom) printf ("case % d:-1 \ n", casenum ++ );}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.