POJ 1324 Holedox Moving (BFS+剪枝)

Holedox Moving
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12103 Accepted: 2899

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

Beijing 2002

 

代码:

//加bfs1()目的是剪枝
//用两次bfs1(),第一次只看蛇头和石头(障碍物),算最小步数minpath.(过程就跟普通的bfs题没什么区别了。。。)
//第二次把蛇的身体也看成蛇头,再用一次bfs1()。。。算最小步数maxpath.
//其实只要bfs()也能过,只不过慢了。两次bfs1()可以把很多情况都筛掉。。。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

struct snake              //存蛇的坐标
{
    int x,y;
}Snake[10],p[1000000];    //Snake[]存身体每节的坐标,p[]存身体变动时蛇的坐标

struct snakeState
{
    snake body[8];       //结构内定义的结构。存蛇身每节的坐标  
    int num;             //蛇身到达此时状态所走的步数
}q[1000000];

int maps[21][21];            
int visit1[21][21];
int visit[21][21][17000];
int movex[4]={-1,1,0,0};    //上下左右
int movey[4]={0,0,-1,1};
int steps[21][21];          //蛇头处于这种状态已经过变动的步数
int n,m,k;
int ans=-1;

//是否能够走。。第一个参数=搜索蛇头走的下一步坐标,第二个参数=此时还未移动的蛇身
int cango(snake &t,snake body[])         
{
    int i;
    if(t.x<=0||t.y<=0||t.x>n||t.y>m||maps[t.x][t.y])    //越界不能走
        return 0;
    for(i=1;i<k;i++)
        if(t.x==body[i].x&&t.y==body[i].y)      //下一步蛇头碰到身体,不能走
            return 0;
    for(i=k-1;i>0;i--)                //更新蛇身的坐标
        body[i]=body[i-1];              
    body[0].x=t.x;                 //更新蛇头的坐标
    body[0].y=t.y;
    return 1;
}

int findmove(snake &a,snake &b)   //判断从b走到a的方向
{
    if(a.x==b.x)
    {
        if(a.y<b.y)
            return 0;  //左
        else
            return 1;  //右
    }
    else
    {
        if(a.x>b.x)
            return 3;  //下
        else
            return 2;  //上
    }
}

//cnt=head  num=rear (数组q相当于一个队列)
void bfs()           
{
    int i,j;
    snakeState a;
    for(i=0;i<k;i++)
        a.body[i]=Snake[i];
    int num=0,cnt=0;           //相当于队列的头指针和尾指针
    a.num=0;
    int state=0;
    for(i=0;i<k-1;i++)                                  //因为状态用0、1、2、3四进制表示
        state=state*4+findmove(Snake[i],Snake[i+1]);    //故蛇身用四进制数表示就是逢4进1(类比10进制啊。。)
    visit[Snake[0].x][Snake[0].y][state]=1;
    q[0]=a;                //入队(前面都是队列初始化)
    num++;                  //尾指针+1
    while(cnt<num)
    {
        snakeState temp=q[cnt];
        cnt++;
        if(temp.body[0].x==1&&temp.body[0].y==1)
        {
            ans=temp.num;
            return;
        }
        for(i=0;i<4;i++)              
        {
            snakeState now=temp;
            snake now1;
            now1.x=now.body[0].x+movex[i];    //now和temp都可以啊。。
            now1.y=now.body[0].y+movey[i];
            if(!cango(now1,now.body))
                continue;
            //now.num=temp.num+1;    
			now.num++;                 //用上面注释的那行也可以,两者等价
            state=0;
            for(j=0;j<k-1;j++)             //蛇身的坐标在cango()函数中更新了,要重新算此时的状态
                state=state*4+findmove(now.body[j],now.body[j+1]);
            if(visit[now.body[0].x][now.body[0].y][state])        //判断此状态下是否走过
                continue;                                         //一般的搜索这里判断都会放在能不能走的函数里,这里比较特殊。
            visit[now.body[0].x][now.body[0].y][state]=1;         //因为判断能不能走时只考虑了蛇头,蛇身坐标和状态还未更新
            if(now.body[0].x==1&&now.body[0].y==1)
            {
                ans=now.num;
                return;
            }
            q[num]=now;      //入队
            num++;           //尾指针+1
        }
    }
}

//只考虑蛇头
void bfs1()            
{
    int num=0,cnt=0;
    p[num]=Snake[0];       //蛇头初始坐标入队
    num++;
    visit1[Snake[0].x][Snake[0].y]=1;
    steps[Snake[0].x][Snake[0].y]=1;      //蛇到初始状态需要步数设为1
    while(cnt<num)
    {
        int i;
        snake temp=p[cnt];
        cnt++;
        if(temp.x==1&&temp.y==1)
        {
            return;
        }
        for(i=0;i<4;i++)
        {
            int tx=temp.x+movex[i];
            int ty=temp.y+movey[i];
            if(tx>=1&&ty>=1&&tx<=n&&ty<=m&&!visit1[tx][ty])
            {
                steps[tx][ty]=steps[temp.x][temp.y]+1;
                if(tx==1&&ty==1)
                    return;
                visit1[tx][ty]=1;
                snake now;
                now.x=tx;
                now.y=ty;
                p[num]=now;
                num++;
            }
        }
    }
}

void show()       //方便debug,囧。。。
{
    int i,j;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
            cout<<steps[i][j]<<" ";
        cout<<endl;
   }
}

int main()
{
    int i,j,Case=0;
    while(scanf("%d%d%d",&n,&m,&k)&&!(n==0&&m==0&&k==0))
    {
        memset(maps,0,sizeof(maps));
        memset(visit,0,sizeof(visit));
        memset(visit1,0,sizeof(visit1));
        memset(steps,0,sizeof(steps));
        for(i=0;i<k;i++)            //读蛇身初始状态的坐标
        {
            scanf("%d%d",&Snake[i].x,&Snake[i].y);
        }
        int djw,x,y;
        ans=-1;
        //show();
        scanf("%d",&djw);          //石头数目
        while(djw--)
        {
            scanf("%d%d",&x,&y);
            maps[x][y]=1;       //是maps[][]=1,不是steps[][]=1
            visit1[x][y]=1;
        }
        bfs1();
		//show();
        if(steps[1][1]==0)       //走不到(1,1)
        {
            printf("Case %d:-1\n",++Case);
            continue;
        }
        int minpath=steps[1][1];     //记录只考虑蛇头和石头时走到(1,1)的最小步数
        memset(steps,0,sizeof(steps));
        for(i=1;i<=n;i++)             //由于刚刚用bfs1()时把蛇头走过的地方visit1[][]都标记为1(不能走)
        {                              //故需要重新初始化地图,将有石头的地方标记为不能走
            for(j=1;j<=m;j++)           
                visit1[i][j]=maps[i][j];
        }
        for(i=1;i<k;i++)
        {
            visit1[Snake[i].x][Snake[i].y]=1;      //把蛇身看成石头
        }
        bfs1();
        int maxpath=steps[1][1];      //记录蛇身看成石头时走到(1,1)的最小步数
        if(minpath==maxpath)
        {
            printf("Case %d: %d\n",++Case,minpath-1);   //因为初始蛇头的位置初始为steps[][]=1,故需要-1
            continue;
        }
        bfs();               //其余情况(筛不了的/剪枝操作无法找到的)再搜一下
        printf("Case %d: %d\n",++Case,ans);
    }
    return 0;
}


 

感想:

代码太长了。。。程序实现和敲代码的能力要加强。。。

 

分析:

state的计算:

定义上下左右分别是0、1、2、3

然后左图B2-B1向左,B3-B2向下,B4-B3向右,表示为三位四进制数为312,转化为10进制就是(2*(4^2))+1*4+3=39.

同理右图B2-B1向下,B3-B2向左,B4-B3向下,表示为121.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值