Holedox Moving

Time limit1sMemory limit128 MB

Summary
Find the minimum number of moves for a snake of length up to 8 to slide its head to the exit at (1,1) on a grid with stones, where the tail cell counts as blocked during a move.
Level

Hard8 of 10

Topics
BFS, Simulation, Bit manipulation, Implementation
Solved
No attempts yet

Problem

Holedox is a small snake that lives in a maze. The maze is a grid of n×mn \times m cells; each cell is either a stone or an empty cell, and Holedox may only travel through empty cells. Cells are addressed by (row, column), and the maze exit is the cell (1, 1).

Holedox's body has length LL and is described block by block as B1(r1,c1) B2(r2,c2) … BL(rL,cL)B_1(r_1, c_1)\ B_2(r_2, c_2)\ \ldots\ B_L(r_L, c_L), where BiB_i is adjacent to Bi+1B_{i+1} for every 1≤i≤L−11 \le i \le L-1. B1B_1 is the head and BLB_L is the tail.

To make one move, Holedox chooses a cell adjacent to its head that is empty — that is, the cell is not a stone and is not currently occupied by any block of its body, the tail included. It moves the head into that cell, and at the same time every other block slides into the cell that the block ahead of it just left: B2B_2 into the old cell of B1B_1, B3B_3 into the old cell of B2B_2, and so on up to BLB_L.

For example, suppose the body is B1(4,1) B2(4,2) B3(3,2) B4(3,1)B_1(4,1)\ B_2(4,2)\ B_3(3,2)\ B_4(3,1). If the only cell the head can move into is (5,1)(5,1), then after one move the body becomes B1(5,1) B2(4,1) B3(4,2) B4(3,2)B_1(5,1)\ B_2(4,1)\ B_3(4,2)\ B_4(3,2).

Given the maze and the starting position of every block of Holedox's body, compute the minimum number of moves the head needs to reach the exit (1, 1).

Input

The input contains several test cases.

Each test case begins with a line of three integers nn, mm (1≤n,m≤201 \le n, m \le 20) and LL (2≤L≤82 \le L \le 8): the number of rows, the number of columns, and the length of Holedox's body. The next LL lines each contain a row and a column, giving the starting positions of B1(r1,c1)B_1(r_1,c_1) through BL(rL,cL)B_L(r_L,c_L) in order, with 1≤ri≤n1 \le r_i \le n and 1≤ci≤m1 \le c_i \le m. The next line contains an integer KK, the number of stones, and the following KK lines each contain the row and column of one stone.

Consecutive test cases are separated by a blank line. The input ends with a line containing three zeros.

It is guaranteed that BiB_i is adjacent to Bi+1B_{i+1} for 1≤i≤L−11 \le i \le L-1, and that the exit cell (1, 1) is never a stone.

Output

For each test case, print a single line Case X: S, where XX is the test-case number (starting from 1) and SS is the minimum number of moves the head needs to reach the exit. Print -1 for SS if the head can never reach the exit.

Hint

In the first example one optimal route for the head is (4,1)→(5,1)→(5,2)→(5,3)→(4,3)→(4,2)→(4,1)→(3,1)→(2,1)→(1,1)(4,1) \to (5,1) \to (5,2) \to (5,3) \to (4,3) \to (4,2) \to (4,1) \to (3,1) \to (2,1) \to (1,1), which takes 9 moves. Note that the head cannot begin by stepping to (3,1)(3,1): even though the tail is about to leave that cell, it is still occupied at the moment of the move.

Examples1

  1. Example 1

    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
    
    Expected output
    Case 1: 9
    Case 2: -1