POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS

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

Sample Output

Case 1: 9
Case 2: -1

题意:
一个M X N的地图 和一个长度为l的蛇  给出蛇各个部位的坐标 和所有障碍物的坐标    求蛇头到达终点的最短距离。

首先想到是根据蛇头BFS遇到障碍或身体则不可行  但这样不可行  因为蛇头可能再次经过已经经过的点时间开销太大
所以应该想一种方法构成标记数组(hash)的第三维元素  由此想到状态压缩  将蛇的形状压缩成一个四进制的数(每一位代表相对前一部分的方向偏移大小)
这样就可以实现去重。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
    int x,y,step;
    int state;
} q[1<<20],head,rear;
int vis[25][25][16384];                  //蛇的形状
int viss[25][25];                        //障碍
int dir[4][2]= {-1,0,0,1,1,0,0,-1};
int bit[3][3];                           //偏移大小数组  x ,y都加了1防止越界
int m,n,len;
void init()
{
    bit[0][1]=0;
    bit[1][2]=1;
    bit[2][1]=2;
    bit[1][0]=3;                         //四方向 一定要和dir数组对应
    memset(vis,0,sizeof(vis));
    memset(viss,0,sizeof(viss));
    return ;
}
void bfs()
{
    int a=0,b=1;
    q[0]=head;
    while(a<b)
    {
        head=q[a++];
        if(head.x==1&&head.y==1)
        {
            cout<<head.step<<endl;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            rear.x=head.x+dir[i][0];
            rear.y=head.y+dir[i][1];
            if(rear.x<1||rear.y<1||rear.x>m||rear.y>n||viss[rear.x][rear.y])
                continue;
            int j,t,tx=head.x,ty=head.y,tstate=head.state;
            rear.state=head.state%(1<<((len-2)*2));                    //去掉四进制的最高位  %1000..(二进制)
            rear.state=rear.state*4+bit[head.x-rear.x+1][head.y-rear.y+1];
            for(j=1; j<len; j++)               //是否碰到身体
            {
                t=tstate%4;
                tstate/=4;
                tx+=dir[t][0];
                ty+=dir[t][1];
                if(tx==rear.x&&ty==rear.y)
                    break;
            }
            if(j<len||vis[rear.x][rear.y][rear.state])
                continue;
            vis[rear.x][rear.y][rear.state]=1;
            rear.step=head.step+1;
            q[b++]=rear;
        }
    }
    cout<<-1<<endl;
    return ;
}
int main()
{
    int i,j,nn,test=0;
    int snake[8][2];
    while(scanf("%d%d%d",&m,&n,&len)&&(m||n||len))
    {
        init();
        int a,b;
        for(i=0; i<len; i++)
            scanf("%d%d",&snake[i][0],&snake[i][1]);
        scanf("%d",&nn);
        while(nn--)
        {
            scanf("%d%d",&a,&b);
            viss[a][b]=1;
        }
        head.state=0;
        for(i=len-1; i>=1; i--)
            head.state=head.state*4+bit[snake[i][0]-snake[i-1][0]+1][snake[i][1]-snake[i-1][1]+1];       //第三为元素 state
        head.x=snake[0][0];
        head.y=snake[0][1];
        head.step=0;
        vis[head.x][head.y][head.state]=1;
        printf("Case %d: ",++test);
        bfs();
    }
    return 0;
}

时间: 2024-08-28 21:58:50

POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS的相关文章

poj 1324 Holedox Moving

poj 1324 Holedox Moving 题目地址: http://poj.org/problem?id=1324 题意: 给出一个矩阵中,一条贪吃蛇,占据L长度的格子, 另外有些格子是石头, 不能通过, 请问蛇到达 (1,1)格子最短距离. 明显的BFS问题, 将每一个可以走的格子进入队列, 格子判断能否走?(给蛇身体标序号(蛇头l, 蛇尾1,其他置为0), 当 fabs(cur_num - tmp_num)>=l 的时候,说明蛇的身体已经离开之前的格子了. #include <ios

POJ 1324 [Holedox Moving] 状态压缩BFS

题目链接:http://poj.org/problem?id=1324 题目大意:n*m网格里有蛇和障碍,蛇只能向空格处移动,不能撞到自己,问到(1,1)的最短步数,如无法到达输出-1. 关键思想:不能直接对蛇头进行BFS,因为蛇身对蛇头的决策也有影响,故BFS时应该保存蛇身状态,我们放在state里用位运算来做.因为根据蛇头和每一节延伸的方向(东南西北)我们可以还原出整条蛇.比较坑的是这道题用pow会超时,自己写一个好啦. 由于vis数组很大,如果对于每个Case都初始化vis数组浪费时间,所

poj 1324 Holedox Moving A*算法对bfs的优化

题意: 迷宫里有一条贪食蛇,求它的蛇头到迷宫左上角最少要多少步. 分析: 关键是将蛇的状态压缩编码,然后bfs,超时就改A*,这题有类似最短路径的性质,A*发现节点重复后不需要更新直接舍弃即可. 代码: //poj 1324 //sep9 #include <iostream> #include <algorithm> #include <queue> using namespace std; struct state { int x[10],y[10]; }; str

poj 1324 状态压缩+bfs

http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17042   Accepted: 4065 Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes

poj 2411 Mondriaan&#39;s Dream(状态压缩+dp)

 题意:用1*2砖块铺满n*m的房间. 思路转自:http://www.cnblogs.com/scau20110726/archive/2013/03/14/2960448.html 因为这道题输入范围在11*11之间,所以可以先打表直接输出.......... 状态压缩DP 经典覆盖问题,输入n和m表示一个n*m的矩形,用1*2的方块进行覆盖,不能重叠,不能越出矩形边界,问完全覆盖完整个矩形有多少种不同的方案 其中n和m均为奇数的话,矩形面积就是奇数,可知是不可能完全覆盖的.接着我们来看

POJ - 2411 Mondriaan&#39;s Dream (状态压缩)

题目大意:要在n * m的网格上面铺满1 * 2或者 2 * 1的砖块,问有多少种铺放的方式 解题思路:刚开始用了3进制表示每行的状态,0表示的是2 * 1的砖块的一部分,1表示的是1 * 2的砖块的上部分,2表示的是1 * 2的砖块的下部分,然后像poj-1185炮兵阵地 那题一样去解决就好了,结果发现状态太多了,会TLE,只得放弃了 后面参考了下别人的代码,可以将其转换成二进制表示形式的,0代表没该位置没被铺到,1代表该位置有被铺到 因为有1 * 2的这种影响两行的砖头存在,所以要判断两个状

hdu 4771 Stealing Harry Potter&#39;s Precious (状态压缩+bfs)

Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1297    Accepted Submission(s): 619 Problem Description Harry Potter has some precious. For example, his invisib

胜利大逃亡(续)(状态压缩bfs)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2552 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

2014 Super Training #6 G Trim the Nails --状态压缩+BFS

原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表示指甲的状态,最多2^20,初始状态为0,表示指甲都没剪,然后BFS找解,每次枚举剪刀的两个方向,枚举移动的位数进行扩展状态即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include &