当前位置:编程学习 > C/C++ >>

poj1324 Holedox Moving

Holedox Moving
Time Limit: 5000MS  Memory Limit: 65536K
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(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 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
     这是一道BFS的题目,但由于此题中蛇可以移动,所以不能通过一个简单的二维标记数组来判重。如:

 

若通过简单的二维标记数组,则此样例无解,显然对于这个图存在最少移动次数9。

   然后我又想着用一个三维数组来标记,即在原先的二维数组的基础上加一个方向,若蛇头经过当前这个格子是来自于另一个方向的话,则入队。但这样做后发现还是存在bug,依旧是上个例子就通不过。

   紧接着又尝试用蛇的头和尾进行判重(即一个四维数组),感觉还行,而且测试数据都能过,但还是wrong了,仔细一想其实还是有问题。

   最后不得不记录整个蛇的状态了,但实在不知道如何压缩状态了。看了解题报告后才明白可以依次记下每一个节点相对前一个节点的位置,这样通过一个三维数组就可以判重了,而且内存也不会爆。

 

[cpp]
#include<iostream>  
#include<cstdio>  
#include<cmath>  
#include<queue>  
#include<cmath>  
#include<cstring>  
 
using namespace std; 
struct st 

    int step; 
    int B[10][2];//蛇的位置  
}w,v; 
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 
int map[21][21];//0表示空地,1表示石头的位置  
bool flag[21][21][16384];//用4进制压缩状态,最大为4^7-1  
int n,m,L,K; 
 
int location(int px,int py,int nx,int ny)//求两个节点的相对位置  

    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])//压缩状态  

    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; 
        &nb

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,