HOJ Holedox Moving——状态压缩的路径记录及判重

来源:互联网 发布:交易英雄金十数据 编辑:程序博客网 时间:2024/06/02 20:09

对于此题的思想我首先表示膜拜:

打死我都想不出来?

题意:

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(4,1) B2(4,2) B3(3,2)B4(3,1). 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 (1,1) 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


Note: In the above sample case, the head of Holedox can follows (4,1)->(5,1)->(5,2)->(5,3)->(4,3)->(4,2)->(4,1)->(3,1)->(2,1)->(1,1) to reach the square of exit with minimal number of step, which is nine.

我们一眼就能看出是个BFS找最短路径的题目,不过在仔细想一下会发现有一个问题比较难解决,当然就是:如何判断当前状态是否已经处理过?如果判断不了的话就会死循环此题就无法解决。

如果采用数组进行存储,就会发现不仅空间的消耗会很大,而且查询判断的时间复杂度也是不允许的。我们想找一种快速而又空间又能够承受的方法进行解决,  细心的会发现,其中有句话说他的长度不会超过8,这个是否提醒我们什么呢?那么我们就可以采取状态压缩的方式进行解决,就是对当前的状态建立一个一一映射,使当前的状态经过转化成为一个数,那么我们的存储空间不仅一下子降下来了,并且查询的方法也会大大的增加,速度最快的是O(1)的,就是开个压缩出数最大值大小的bool数组,保存状态即可。如果如果压缩的数过大,也可采用稍次一点的O(logN)的hash表来进行存储。这个可以大大的节省空间,而且时间复杂度也是相当低的。

针对此题,我们发现蛇的长度最多是8,现在我们以蛇头为基准进行处理:蛇神是连续的,他们对于他们 前方的蛇身无非就是上下左右的位置。

如果我们规定上下左右为0、1、2、3,那么以蛇头为基准就会得到一个连续的四进制数去掉蛇头最多一共7位

现在还要加上蛇头的位置,我们放在最高位.蛇头是一个二维的坐标:(x,y)那么我们为了实现一一映射必须保证进制足够大。我们发现格子限定最大不超过20,那么为了节省空间下标我们取从0~19。那么我们可疑规定蛇头的压缩为:20x+y那么就可以得出蛇长为8,20*20方格的压缩值为:

(20*x+y)*4^7+a1+a2*4^1+a3*4^2+......+a6*4^6.

在使用此思想时,一定要注意此数据的范围选择相应的数据存储类型。

那么其他的情况依次类推出来即可。

后面待补:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


原创粉丝点击