poj 1324 Holedox Moving(bfs+状态压缩)

来源:互联网 发布:dota2新英雄语音知乎 编辑:程序博客网 时间:2024/06/01 09:43

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 44 14 23 23 132 33 33 44 4 42 31 31 42 442 12 23 44 20 0 0

Sample Output

Case 1: 9Case 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. 

记录蛇头位置,然后用两个二进制位从高到底分别记录当前蛇的部分,与他前一部分·的相对方向,更新状态时可以通过移位解决。主要是状态压缩这里,写的太少,费了点劲才搞定,还参考了别人的代码。

#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <queue>#define INF 100000000using namespace std;int n,m,L;bool map[22][22];int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};bool vis[22][22][1<<15];struct Point{    int x;    int y;}point[10];struct node{    int x;    int y;    int step;    int staus;    node(int x=0,int y=0,int step=0,int staus=0)    {        this->x=x;        this->y=y;        this->step=step;        this->staus=staus;    }};int getstart(){    int flag=0,dx,dy,staus=0;    for (int i=L-1; i>0; i--)    {        //第i个点相对于第i-1个点的位置        dx=point[i].x-point[i-1].x;        dy=point[i].y-point[i-1].y;        if(dx == 0 && dy == 1)            flag = 1;  //编码表示        else if(dx == 0 && dy == -1)            flag = 3;        else if(dx == -1 && dy == 0)            flag = 0;        else if(dx == 1 && dy == 0)            flag = 2;                staus=staus<<2;        staus|=flag;    }    return staus;}int get_staus(int i,int staus){    int flag;    int k=(1 << ((L - 1) << 1)) - 1;    int dx=0,dy=0;        //第二个点相对于第一个点(头)的位置    dx=dx-dir[i][0];    dy=dy-dir[i][1];        if(dx == 0 && dy == 1)        flag = 1;    else if(dx == 0 && dy == -1)        flag = 3;    else if(dx == -1 && dy == 0)        flag = 0;    else if(dx == 1 && dy == 0)        flag = 2;    staus = staus << 2; //所有的位置更新(依次移位就可以了)    staus = staus | flag; //更新第二个点的位置    staus = staus & k; // 去除高位部分    return staus;}bool check(int x,int y,int prex,int prey,int staus){    int flag;    for (int i=0; i<L-1; i++)    {        flag=3;        flag=flag&staus; //取出第i点的相对位置        staus=staus>>2;  //移位        if(x==prex+dir[flag][0] && y==prey+dir[flag][1]) //得出实际位置,并判断是否与头冲突 (注意编码与方向数组的一致性)            return false;        prex+=dir[flag][0];        prey+=dir[flag][1];    }    return true;}int bfs(){    queue<node>q;    int staus=getstart();    q.push(node(point[0].x,point[0].y,0,staus));    vis[point[0].x][point[0].y][staus]=1;    node temp,a;    while (!q.empty())    {        a=q.front();        q.pop();        if(a.x==1 && a.y==1)        {            return a.step;        }        for (int i=0; i<4; i++)        {            temp=a;            temp.x+=dir[i][0];            temp.y+=dir[i][1];            temp.step++;            staus=get_staus(i, temp.staus);  //获得当前状态            if(temp.x>0 && temp.x<=n && temp.y>0 && temp.y<=m && !vis[temp.x][temp.y][staus] && !map[temp.x][temp.y])            {                if(check(temp.x, temp.y, a.x, a.y,temp.staus)) //检查是否冲突                {                           vis[temp.x][temp.y][staus]=1;                    temp.staus=staus;                    q.push(temp);                }            }        }    }    return -1;}int main(){    int u,v,k;    int cnt=0;    while (scanf("%d%d%d",&n,&m,&L) && n+m+L)    {        memset(map, 0, sizeof(map));        memset(vis, false, sizeof(vis));        for (int i=0; i<L; i++)            scanf("%d%d",&point[i].x,&point[i].y);                    scanf("%d",&k);        for (int i=0; i<k; i++)        {            scanf("%d%d",&u,&v);            map[u][v]=1;        }        printf("Case %d: %d\n", ++cnt, bfs());            }    return 0;}



0 0
原创粉丝点击