【POJ】1324Holedox Moving(贪吃蛇的bfs)

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 18421 Accepted: 4365

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. 

 

题目大意:

给出一个n*m的贪吃蛇地图,以及贪吃蛇现在身体各个块所在的位置,求它的头走到(1,1)位置最少需要多少步。

        注意:蛇在移动的过程中不能走到障碍物上,也不能撞到自己的身体上。

思路:参考自https://blog.csdn.net/u013480600/article/details/25336473 感谢大佬!!

直接BFS,不过这里的vis状态表示要注意。由于蛇最长为8格。所以我们用vis[21][21][1<<14] 表示蛇的这种状态是否已经出现.其中vis的前两维表示蛇头的坐标,后一维的二进制形式的每2位(可表0-3)表示从1到L-1开始该蛇的身体在该蛇身体的前一格的方向.

        这样我们就能用最小的空间表示完整个蛇在迷宫的状态了.

        现在要求最小距离,我们不用dist[][][]了,因为太耗空间了.我们用Node节点,Node中有x,y,st,dist  4个属性,前3个属性对应vis数组的前三维.最后一个属性是当前状态的最小距离.

        在BFS扩展的时候,对于4个方向,计算得到nx和ny,然后判断nx和ny是否越界,是否是障碍,是否会与蛇的旧身体位置冲突.如果以上情况都不会发生,那么就生成了一个蛇与迷宫的新状态.

        注意,由于vis数组很大,如果对于每个kase都初始化vis数组浪费时间,所以在申请了全局变量vis之后,我们对于每个kase,都只用当前的kase值去标记vis表示当前状态已出现.

        注意这里记录的st状态中的方向是指,后面一个格子(蛇的身体)处于前面一个格子(蛇的身体)的哪个方向.所以在BFS的时候,那个d要取反方向才能加到新的nst中

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxr=21;
const int maxc=21;

int vis[maxr][maxc][1<<14];
int maze[maxr][maxc];

int R,C;
struct Node
{
    int x,y,st,dist;
    Node(int x,int y,int st,int dist):x(x),y(y),st(st),dist(dist){}

};

int L;
int kase=0;
int dir[10];
int dx[]={1,0,-1,0};
int dy[]={0,-1,0,1};

bool check(int x,int y,Node node)
{
    for(int i=L-1;i>=1;i--)
    {
        dir[i]=node.st&3;
        node.st>>=2;
    }

    int xx=node.x,yy=node.y;

    for(int i=1;i<L;i++)
    {
        xx+= dx[dir[i]];
        yy+= dy[dir[i]];
       if(xx==x&&yy==y) return true;//表示冲突

    }
    return false;
}

queue<Node> Q;
int BFS(Node nod)
{
    kase++;
    if(nod.x==1&&nod.y==1) return 0;
    while(!Q.empty()) Q.pop();

    Q.push(nod);
    vis[nod.x][nod.y][nod.st]=kase;

    while(!Q.empty())
    {
        Node node=Q.front();
        Q.pop();
        int x=node.x,y=node.y,st=node.st,dist=node.dist;

        for(int d=0;d<4;d++)
        {
            int nx=x+dx[d];
            int ny=y+dy[d];

            if(nx==1&&ny==1) return dist+1;
            if(nx<1||nx>R||ny<1||ny>C||maze[nx][ny]==1||check(nx,ny,node)) continue;

            int ndist=dist+1,nst=(st>>2)+( ((d+2)%4)<<(2*(L-2)));
            if(vis[nx][ny][nst]==kase)continue;
            Q.push(Node(nx,ny,nst,ndist));
            vis[nx][ny][nst]=kase;
        }
    }
    return -1;
}


int main()
{
    while(scanf("%d%d%d",&R,&C,&L)==3)
    {
        if(R+C+L==0) break;

        int x,y,nx,ny;
        Node node(0,0,0,0);
        scanf("%d%d",&node.x,&node.y);
        x=node.x,y=node.y;
        for(int i=1;i<L;i++)
        {
            scanf("%d%d",&nx,&ny);
            for(int d=0;d<4;d++)
            {
                if(x+dx[d]==nx && y+dy[d]==ny)
                {
                    node.st = (node.st<<2)+d;
                    break;
                }
            }
            x=nx,y=ny;
        }

        int blocks;
        scanf("%d",&blocks);
        memset(maze,0,sizeof(maze));
        for(int i=1;i<=blocks;i++)
        {
            scanf("%d%d",&x,&y);
            maze[x][y]=1;
        }
        printf("Case %d: %d\n",kase,BFS(node));
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值