1324 Holedox Moving 贪吃蛇问题

Holedox Moving
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7703 Accepted: 1891

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

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.

Source

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int base=500000;
bool flag[6553600],vis[25][25];
int n,m,l;
int xx[4]={-1,0,1,0};
int yy[4]={0,1,0,-1};
int front,rear;//队列头和尾
struct Node
{
    int x,y;//头
    int step;//步数
    int s[8];//相对上一个位置的状态,不包括蛇头
    int p[8][2];//蛇状态
};
Node q[base];
int _pre(int sx,int sy,int _x,int _y)//相对上个位置的状态
{
     int x=_x-sx,y=_y-sy;
     if(x==-1) return 1;
     else if(x==1) return 3;
     else if(y==-1) return 2;
     return 0;
}
int _val(Node G)
{
    int sum=20*G.x+G.y;//20  important
    for(int i=0;i<l-1;i++) sum=sum*4+G.s[i];
    return sum;
}
int itself(int x,int y,Node G)//不能和自身相撞
{
    for(int i=0;i<l-1;i++)
    {
        if(x==G.p[i][0]&&y==G.p[i][1]) return 1;
    }
    return 0;
}
int bfs()
{
    while(front!=rear)
    {
        front=(front+1)%base;
        int v=_val(q[front]);
        if(flag[v]) continue;
        flag[v]=1;
        if(q[front].x==0&&q[front].y==0) return q[front].step;
        for(int i=0;i<4;i++)
        {
            int x,y;
            x=q[front].x+xx[i],y=q[front].y+yy[i];
            if(x<0 || y<0 || x>=n || y>=m || vis[x][y]) continue;
            if(itself(x,y,q[front])) continue;
            rear=(rear+1)%base;
            q[rear].x=x,q[rear].y=y,q[rear].step=q[front].step+1;
            for(int j=0;j<l-2;j++)
            {
                q[rear].s[j+1]= q[front].s[j];//向前走一格
                q[rear].p[j+1][0]=q[front].p[j][0];
                q[rear].p[j+1][1]=q[front].p[j][1];
            }
            q[rear].p[0][0]=q[front].x,q[rear].p[0][1]=q[front].y;
            q[rear].s[0]=_pre(x,y,q[front].x,q[front].y);
        }
    }
    return -1;
}
int main()
{
    int ci=1;
    while(scanf("%d%d%d",&n,&m,&l)==3&&n)
    {
        memset(vis,0,sizeof(vis));
        memset(flag,0,sizeof(flag));
        int x,y;scanf("%d%d",&x,&y);x--,y--;//头
        front=-1,rear=0;//队列头和尾
        q[0].x=x,q[0].y=y,q[0].step=0;
        int sx=x,sy=y;
        for(int i=0;i<l-1;i++)
        {
            scanf("%d%d",&x,&y);x--,y--;
            q[0].s[i]=_pre(sx,sy,x,y);
            q[0].p[i][0]=x,q[0].p[i][1]=y;//蛇身
            sx=x,sy=y;
        }
        int k;scanf("%d",&k);
        while(k--)
        {
            scanf("%d%d",&x,&y);x--,y--;
            vis[x][y]=1;
        }
        cout<<"Case "<<ci++<<": ";
        int cnt=bfs();
        printf("%d/n",cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值