Holedox Moving
| Time Limit: 5000MS |
|
Memory Limit: 65536K |
| Total Submissions: 12148 |
|
Accepted: 2905 |
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。判斷圖中重複的方法是貪吃蛇處於相同的位置(每一個點都在相同的位置),用1個3維數組來儲存。用STL裡的隊列會TLE;可以用數組類比隊列,也很簡單。注意DFS時必須判斷蛇頭是不是已經在(1,1)了,我最開始沒判斷WA了幾次;AC代碼:
#include<stdio.h>#include<vector>#include<queue>#include<cmath>using namespace std;struct node{int x,y;};struct snake_locat{int dis;int body[8][2];}w,v,q[5000004];int n,m,l;node add[4];bool a[21][21];bool b[21][21][17000];bool check(node &nn,snake_locat &t){if(nn.x>=1&&nn.x<=n&&nn.y>=1&&nn.y<=m&&a[nn.x][nn.y]==0){for(int i=0;i<l;i++){if(nn.x==t.body[i][0]&&nn.y==t.body[i][1])return 0;}return 1;}return 0;}int position(int &nx,int &ny,int &px,int &py){if(nx==px){if(ny>py)return 0;elsereturn 1;}else{if(nx>px)return 2;elsereturn 3;}}int get_position(snake_locat& now)//把整條蛇的相對位置轉化為一個4進位數{int s=0,i;for(i=0;i<l-1;i++)s=s*4+position(now.body[i][0],now.body[i][1],now.body[i+1][0],now.body[i+1][1]);return s;}int bfs(void){if(w.body[0][0]==1&&w.body[0][1]==1)return 0;int i,j,head,tair;head=tair=0;w.dis=0;q[tair]=w;tair++;int sta;sta=get_position(w);b[w.body[0][0]][w.body[0][1]][sta]=1;while(head<tair){w=q[head++];for(i=0;i<4;i++){node nn;nn.x=w.body[0][0]+add[i].x;nn.y=w.body[0][1]+add[i].y;if(nn.x==1&&nn.y==1)return w.dis+1;if(check(nn,w)){v.dis=w.dis+1;v.body[0][0]=nn.x;v.body[0][1]=nn.y;for(j=l-1;j>0;j--){v.body[j][0]=w.body[j-1][0];v.body[j][1]=w.body[j-1][1];}int ck;ck=get_position(v);if(b[nn.x][nn.y][ck]==0){q[tair++]=v;b[nn.x][nn.y][ck]=1;}}}}return -1;}int main(){int i,ca=0;add[0].x=1;add[0].y=0;add[1].x=0;add[1].y=1;add[2].x=-1;add[2].y=0;add[3].x=0;add[3].y=-1;while(scanf("%d%d%d",&n,&m,&l)&&n&&m&&l){ca++;memset(a,0,sizeof(a));memset(b,0,sizeof(b));for(i=0;i<l;i++){scanf("%d%d",&w.body[i][0],&w.body[i][1]);}int block;scanf("%d",&block);for(i=0;i<block;i++){int x,y;scanf("%d%d",&x,&y);a[x][y]=1;}int sum;sum=bfs();printf("Case %d: %d\n",ca,sum);}return 0;}