poj1324Holedox Moving【广搜。状态压缩】

Holedox Moving
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 14912 Accepted: 3603

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

[Submit]   [Go Back]   [Status]   [

最初看这个题是一点没有思路啊,网格中间一条蛇,还有一堆障碍,问你蛇怎么移动,讨论区说是状态压缩,压缩你妹啊20x20的格子,最长为8个单位的蛇,这玩意咋写啊,百思不得其解的我看了题解,哇咔咔,状态压缩做的是蛇身每一节相对于前一节的位置,哇卡卡卡居然还可以这么玩

再有就是注意细节,好久没写广搜了QAQ,手生,一堆小问题委屈


/**************
poj1324
2016.2.27
32752K	1891MS	C++	2841B
**************/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxs 1<<14
#define maxn 22
#define inf 1<<29
struct state{
    int x,y,dis,s;
    state(int x=0,int y=0,int dis=0,int s=0):x(x),y(y),dis(dis),s(s){};
};
struct point{
    int x,y;
};
int n,m,res,l;
int vis[maxn][maxn][maxs];
int fx[4]={-1,0,1,0};
int fy[4]={0,1,0,-1};
bool map[maxn][maxn];
point pos[maxn*maxn];
queue<state>q;
int get_start()
{
    int s=0;
    for(int i=l-1;i>=1;i--)
    {
        s<<=2;
        if(pos[i].x==pos[i-1].x)
        {
            if(pos[i].y>pos[i-1].y) s+=1;
            else s+=3;
        }
        else if(pos[i].y==pos[i-1].y)
        {
            if(pos[i].x>pos[i-1].x) s+=2;
        }

    }
    return s;
}
int get_next_state(int i, int s)
{
    int k = (1 << ((l - 1) << 1)) - 1;
    s<<=2;//理解本质啊啊啊啊 而且是<<=  !!
    if(fx[i]==0)
    {
        if(fy[i]>0) s=3+s;
        else s=1+s;
    }
    else if(fy[i]==0)
    {
        if(fx[i]<0) s=2+s;
    }
    s=s&k;
    return s;
}
bool judge_code(int x, int y, int pre_x, int pre_y, int s)
{
    int dir;
    for(int i=0;i<l-1;i++)
    {
        dir=3;
        dir=s&dir;
        s>>=2;//怎么能是乘以4呢???
        if(x==pre_x+fx[dir]&&y==pre_y+fy[dir])
            return false;
        pre_x+=fx[dir],pre_y+=fy[dir];
    }
    return true;
}
void BFS()
{
    state a;
    int dx,dy,s;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=0;i<4;i++)//你是多久没写光搜了== 范围还能写成<n  !!
        {
            dx=a.x+fx[i],dy=a.y+fy[i];
            s=get_next_state(i,a.s);
            if(dx>0&&dx<=n&&dy>0&&dy<=m&&!vis[dx][dy][s]&&!map[dx][dy]&&judge_code(dx,dy,a.x,a.y,a.s))
            {
                if(dx==1&&dy==1)
                {
                    res=a.dis+1;
                    return;
                }
                vis[dx][dy][s]=1;
                q.push(state(dx,dy,a.dis+1,s));
            }
        }
    }
}
int main()
{
    freopen("cin.txt","r",stdin);
    int cas=1;
    while(~scanf("%d%d%d",&n,&m,&l)&&n)
    {
        for(int i=0;i<l;i++)
            scanf("%d%d",&pos[i].x,&pos[i].y);
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        scanf("%d",&res);
        while(res--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x][y]=1;
        }
        if(pos[0].x==1&&pos[0].y==1)
        {
            printf("Case %d: 0\n",cas++);
            continue;
        }
        int ss=get_start();
        q.push(state(pos[0].x,pos[0].y,0,ss));
        vis[pos[0].x][pos[0].y][ss]=1;
        res=inf;
        BFS();
        if(res!=inf)
            printf("Case %d: %d\n",cas++,res);
        else printf("Case %d: -1\n",cas++);
        while(!q.empty()) q.pop();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值