1. 程式人生 > >【POJ】1324Holedox Moving(貪吃蛇的bfs)

【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;
}