POJ - 1324 Holedox Moving【状态压缩+A*】

原创 2018年04月17日 17:48:35

Holedox Moving
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 17742 Accepted: 4175


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). 


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. 


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
2 3
3 3
3 4

4 4 4
2 3
1 3
1 4
2 4

2 1
2 2
3 4
4 2

0 0 0

Sample Output

Case 1: 9
Case 2: -1


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. 






using namespace std;
#define inf 0x3f3f3f3f
int n,m,L,beg[10][2],G[22][22];
bool vis[22][22][1<<14];
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
struct node
    int y,x,step,h,g;
    int body[10][2];
    bool operator <(node a)const
        return a.step+a.g<step+g;
int getf(int sty,int stx,int edy,int edx)
    int ty=edy-sty;
    int tx=edx-stx;
    for(int i=0; i<4; i++)
        if(ty==dir[i][0] && tx==dir[i][1])
            return i;
int bfs()
    priority_queue<node> q;
    memset(vis,0,sizeof vis);
    int h=0;
    for(int i=1; i<L; i++)
    memcpy(p.body,beg,sizeof beg);
        node top=q.top(); q.pop();
        if(top.y==0 && top.x==0) return top.step;
        for(int i=0;i<4;i++)
            int ty=top.y+dir[i][0];
            int tx=top.x+dir[i][1];
            if(ty<0 || ty>=n || tx<0 || tx>=m || G[ty][tx])
            int flag=0;
            for(int j=0;j<L;j++)
                if(ty==top.body[j][0] && tx==top.body[j][1])
            if(flag) continue;
            int h=(top.h>>2)+i*(1<<(2*L-4));
            for(int j=1;j<L;j++)

            if(vis[ty][tx][h]) continue;
    return -1;
int main()
    int cas=1;
    while(scanf("%d%d%d",&n,&m,&L) && n+m+L)
        for(int i=0; i<L; i++)
        int k;
        memset(G,0,sizeof G);
        for(int i=0;i<k;i++)
            int y,x;
        printf("Case %d: %d\n",cas++,bfs());
    return 0;

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013852115/article/details/79977972

POJ 1324Holedox Moving (状态BFS)

Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15264   Accepted: ...
  • tomorrowtodie
  • tomorrowtodie
  • 2016-07-07 00:18:57
  • 482

poj1324 Holedox Moving

参考:http://www.cnblogs.com/kedebug/archive/2013/03/26/2983143.html 大致题意: 给定一个地图和一条蛇,问蛇需要最少走多少步才能走到出口...
  • ctxdecs
  • ctxdecs
  • 2014-04-23 16:20:52
  • 880

POJ 1324 Holedox Moving(A*+状态压缩)

// // main.cpp // Richard // // Created by 邵金杰 on 16/8/25. // Copyright © 2016年 邵金杰. All rights ...
  • qq_33901573
  • qq_33901573
  • 2016-08-25 08:55:53
  • 97

POJ 1324 Holedox Moving 状态压缩+A* -

题目地址:http://poj.org/problem?id=1324 写了一天了 ,一开始想表示状态用头和尾位置,但很快写出来就WR 然后就用根据贪吃蛇的节点与上一个节点位置关系保存状态,一共有...
  • qq_34446253
  • qq_34446253
  • 2016-09-02 21:44:20
  • 128

Poj 1324 Holedox Moving (A*)

题目链接:http://poj.org/problem?id=1324   题意:给出当前地图和蛇的状态,问蛇到出口最少需要多少步。   根据蛇各部位之间的关系,用位来存储蛇的状态,而地图上每...
  • dooder_daodao
  • dooder_daodao
  • 2011-08-13 11:07:58
  • 930

poj 1324 Holedox Moving

 //poj 1324 Holedox Moving 贪食蛇(bfs+剪枝)/* 题解:http://blog.sina.com.cn/s/blog_520db5ec0100coqb.html上面这位...
  • birdforever
  • birdforever
  • 2010-08-26 00:27:00
  • 1067

POJ - 1324 Holedox Moving

题意:贪吃蛇的题目 思路:BFS+状态的记录,坑了无数发,#include #include #include using namespace std; const int MAXN = 500000...
  • u011345136
  • u011345136
  • 2014-06-06 16:34:55
  • 834

poj 1324 Holedox Moving

Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13428  ...
  • u012866104
  • u012866104
  • 2014-08-28 22:55:44
  • 356

POJ 1324 Holedox Moving

Name: Holedox Moving P_ID: POJ 1324 题目链接:http://poj.org/problem?id=1324题意描述: n*m(最大20)的迷宫,l长(不超过8...
  • lvxin1204
  • lvxin1204
  • 2016-04-27 15:54:29
  • 137

POJ 1324 Holedox Moving

Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12148  ...
  • makaihong123
  • makaihong123
  • 2013-07-23 21:15:37
  • 504
您举报文章:POJ - 1324 Holedox Moving【状态压缩+A*】