Holedox Moving POJ - 1324

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

Hint

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.

题意:

在一个有障碍物的迷宫中,有一条蛇在移动。题目给出蛇头和蛇身的每个坐标,给出终点和障碍物坐标,问这条蛇能不能到达终点(蛇头到达)。如果能,输出最短路径的长度,如果不能,输出-1。

分析:

首先我们第一想法很容易想到是BFS,然后我们要想一下怎么判断某一状态的蛇身是否标记过了。最直白的想法就是使用一个数组将蛇头和每一节蛇身保存起来。这个做法在结果上来看是可行的,但是耗费的内存和时间太多了,显然是不现实的。

所以,我们这里要使用状态压缩。我们知道,后一节蛇身都是跟随前一节蛇身移动的。比如第二节蛇身的坐标是(1,1),那么在蛇移动了一步之后第三节蛇身的坐标就是(1,1),因此,我们可以使用蛇头的坐标和后一节蛇身对于前一节蛇身的相对位置推算出整条蛇的状态。而蛇身移动的方向只有上下左右四个方向,再加上蛇身并不长,因此我们可以使用一个四进制数来表示蛇的状态。

解决了表示蛇的状态的问题,剩下的按照BFS的步骤求解就可以了。

#include<iostream>
#include<cstring>
#include<queue>
#define mem(arr,p) memset(arr,p,sizeof(arr))
using namespace std;
const int maxn=22;
typedef long long ll;
int _map[maxn][maxn];//地图
int dict[4][2]= {-1,0,1,0,0,-1,0,1};// 对应着蛇头的上下左右四个移动方向
// 注意上下左右的顺序要与下面的dis数组一致
// 这里dict数组中的顺序是左右下上,所以dis数组中对应的顺序也是左右上下
bool vis[maxn][maxn][1<<14];// 判断某一状态的蛇是否标记过
int dis[3][3];// 进行四进制转化是使用的数组
int n,m,len;// 对应着地图的长,宽,蛇身长度
struct snake
{
    int x[10],y[10];// 保存的蛇的坐标(蛇头和每一节蛇身)
    int cnt,k;// 对应着蛇移动的步数和蛇的状态对应的四进制数
} in;
void init()
{
    dis[0][1]=0;
    dis[2][1]=3;
    dis[1][2]=1;
    dis[1][0]=2;
}
bool judge(int x,int y,snake p)// 判断蛇移动一步后的状态是否存在
{
    if(_map[x][y])// 新蛇头的位置是障碍物
        return false;
    for(int i=1; i<=len; i++)
        if(x==p.x[i]&&y==p.y[i])// 新蛇头的位置是原来的蛇身
            return false;
    return true;
}
void update(snake &p,snake q)// 更新蛇身
{
    for(int i=2; i<=len; i++)
    {
        p.x[i]=q.x[i-1];
        p.y[i]=q.y[i-1];
    }
}
int B_con(snake p,int k)// 将蛇的状态压缩成一个四进制数
{
    int ans=0;
    for(int i=len-1; i>1; i--)
        ans=ans*4+dis[p.x[i]-p.x[i-1]+1][p.y[i]-p.y[i-1]+1];
    ans=ans*4+dis[1-dict[k][0]][1-dict[k][1]];
    return ans;
}
int bfs()
{
    mem(vis,false);
    queue<snake>num;
    snake s;
    num.push(in);
    vis[in.x[1]][in.y[1]][in.k]=true;
    while(!num.empty())
    {
        in=num.front();
        num.pop();
        if(in.x[1]==1&&in.y[1]==1)
            return in.cnt;
        for(int i=0; i<4; i++)
        {
            s=in;
            s.x[1]+=dict[i][0];
            s.y[1]+=dict[i][1];
            s.cnt++;
            if(!judge(s.x[1],s.y[1],in))
                continue;
            if(s.x[1]>=1&&s.x[1]<=n&&s.y[1]>=1&&s.y[1]<=m)
            {
                if(s.x[1]==1&&s.y[1]==1)
                    return s.cnt;
                update(s,in);
                s.k=B_con(s,i);
                if(vis[s.x[1]][s.y[1]][s.k])
                    continue;
                vis[s.x[1]][s.y[1]][s.k]=true;
                num.push(s);
            }
        }
    }
    return -1;
}
int main()
{
    int p=0,q;
    init();
    while(cin>>n>>m>>len&&(n||m||len))
    {
        for(int i=1; i<=len; i++)
            cin>>in.x[i]>>in.y[i];
        cin>>q;
        mem(_map,0);
        for(int i=0; i<q; i++)
        {
            int x,y;
            cin>>x>>y;
            _map[x][y]=1;
        }
        int ans=0;
        for(int i=len; i>1; i--)
            ans=ans*4+dis[in.x[i]-in.x[i-1]+1][in.y[i]-in.y[i-1]+1];
        in.k=ans;
        in.cnt=0;
        cout<<"Case "<<++p<<": "<<bfs()<<endl;
    }
}
/*
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
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值