POJ1324 Holedox Moving BFS+状压

记录蛇头位置,然后用两个二进制位从高到底分别记录当前蛇的部分,与他前一部分·的相对方向,然后就是一个普通的BFS了。



Holedox Moving
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 11994 Accepted: 2878

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

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdio>

using namespace std;

struct state
{
    int x[10],y[10];
};

struct node
{
    int x,y,s,k;
};

state s;
int n,m,l,k;
int vis[22][22][1<<15];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int g[25][25];

state decode(int x,int y,int s,int l)
{
    int dire;
    state pos;
    pos.x[0]=x,pos.y[0]=y;
    for(int i=1;i<l;i++)
    {
        dire=3;
        dire&=s;
        s>>=2;
        pos.x[i]=pos.x[i-1]+dir[dire][0];
        pos.y[i]=pos.y[i-1]+dir[dire][1];
    }
    return pos;
}

int encode(state s,int l)
{
    int st=0;
    for(int i=l-1;i>0;i--)
    {
        int x,y,now;
        x=s.x[i]-s.x[i-1];
        y=s.y[i]-s.y[i-1];
        if(x==0&&y==1)
            now=2;
        else if(x==0&&y==-1)
            now=3;
        else if(x==1&&y==0)
            now=0;
        else if(x==-1&&y==0)
            now=1;
        st=st<<2;
        st|=now;
    }
    return st;
}

node moves(node s,int d,int l)
{
    int mod=(1<<((l-1)*2))-1;
    int now;
    int nx,ny,x,y;
    nx=s.x+dir[d][0];
    ny=s.y+dir[d][1];
    x=-dir[d][0];y=-dir[d][1];
    if(x==0&&y==1)
            now=2;
    else if(x==0&&y==-1)
            now=3;
    else if(x==1&&y==0)
            now=0;
    else if(x==-1&&y==0)
            now=1;
    s.s<<=2;
    s.s|=now;
    s.s&=mod;
    s.x=nx,s.y=ny;
    return s;
}

bool jud(int x,int y,int s,node pre)
{
    if(x<1||x>n||y<1||y>m) return false;
    if(vis[x][y][s]==1) return false;
    if(g[x][y]==1) return false;
    state ss=decode(pre.x,pre.y,pre.s,l);
    for(int i=0;i<l;i++)
        if(ss.x[i]==x && ss.y[i]==y) return false;
    return true;
}

int bfs()
{
    queue<node> q;
    node a,tp;
    a.x=s.x[0],a.y=s.y[0];
    a.s=encode(s,l);
    a.k=0;
    q.push(a);
    vis[a.x][a.y][a.s]=1;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        state tmp;
        tmp=decode(a.x,a.y,a.s,l);
        if(a.x==1 && a.y==1)
            return a.k;
        for(int i=0;i<4;i++)
        {
            tp=moves(a,i,l);
            tp.k=a.k+1;
            if(!jud(tp.x,tp.y,tp.s,a)) continue;
            vis[tp.x][tp.y][tp.s]=1;
            q.push(tp);
        }
    }
    return -1;

}

int main()
{
    int cs=1;
    while(cin>>n>>m>>l)
    {
        if(n+m+l==0) break;
        for(int i=0;i<l;i++)
            cin>>s.x[i]>>s.y[i];
        cin>>k;
        int u,v;
        memset(g,0,sizeof(g));
        for(int i=0;i<k;i++)
        {
            cin>>u>>v;
            g[u][v]=1;
        }
        memset(vis,0,sizeof(vis));
        int mins=bfs();
        printf("Case %d: %d\n",cs++,mins);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值