POJ-1324-Holedox Moving(BFS)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   

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. 

Source

Beijing 2002


思路:從頭部開始想尾部走,不同方向代表不同的值,就得到蛇的狀態,然後就是普通的BFS,還要注意不能咬到自己,首尾相連也不行。


#include <stdio.h>struct S{int step,x[9],y[9],head;}que[1000000];bool vis[20][20][16384];int mp[20][20],nxt[4][2]={{1,0},{0,-1},{0,1},{-1,0}},mi[8]={1,4,16,64,156,1024,4096,16384};int main(){    int n,m,l,i,j,k,x,y,head,temp,lx,ly,tl,valx,valy,casenum=1,top,bottom;    while(~scanf("%d%d%d",&n,&m,&l) && n)    {        for(i=0;i<l;i++)        {            scanf("%d%d",&x,&y);            que[0].x[i]=x-1;            que[0].y[i]=y-1;        }        for(i=0;i<n;i++) for(j=0;j<m;j++) mp[i][j]=1;        for(i=0;i<n;i++) for(j=0;j<m;j++) for(k=0;k<mi[l-1];k++) vis[i][j][k]=0;        scanf("%d",&k);        while(k--)        {            scanf("%d%d",&x,&y);            mp[x-1][y-1]=0;        }        que[0].step=0;        que[0].head=0;        top=0;        bottom=1;        while(top<bottom)        {            if(!que[top].x[que[top].head] && !que[top].y[que[top].head])            {                printf("Case %d: %d\n",casenum++,que[top].step);                break;            }            head=que[top].head-1;            if(head==-1) head=l;            que[top].step++;            for(i=0;i<4;i++)            {                x=que[top].x[head]=que[top].x[que[top].head]+nxt[i][0];                y=que[top].y[head]=que[top].y[que[top].head]+nxt[i][1];                if(x>=0 && x<n && y>=0 && y<m && mp[x][y])                {                    temp=0;                    lx=x;                    ly=y;                    tl=0;                    for(j=que[top].head;tl<l;j++)                    {                        if(j==l+1) j=0;                        if(que[top].x[j]==x && que[top].y[j]==y) break;//如果會咬到自己                        if(tl<l-1)//計算狀態                        {                            valx=lx-que[top].x[j];                            valy=ly-que[top].y[j];                            if(valx==0 && valy==1) temp=temp<<2;                            else if(valx==1 && valy==0) temp=temp<<2|1;                            else if(valx==-1 && valy==0) temp=temp<<2|2;                            else if(valx==0 && valy==-1) temp=temp<<2|3;                            lx=que[top].x[j];                            ly=que[top].y[j];                        }                        tl++;                    }                    if(tl==l && !vis[x][y][temp])//如果不會咬到自己並且該狀態沒有訪問過                    {                        vis[x][y][temp]=1;                        que[top].head--;                        if(que[top].head==-1) que[top].head=l;                        que[top].x[head]=x;                        que[top].y[head]=y;                        que[bottom++]=que[top];                        que[top].head++;                        if(que[top].head==l+1) que[top].head=0;                    }                }            }            top++;        }        if(top==bottom) printf("Case %d: -1\n",casenum++);    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.