Holedox Moving
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 16057 | Accepted: 3892 |
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).
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.
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
题意:
在n*m的地图上,给出长度为L的蛇身体各个节位置,以及有k个点是墙,问蛇从初始位置走到(1,1)点的最小步数。蛇不能撞墙,不能撞自己的身体。
代码:
//思路就是简单的bfs,但是蛇的状态不好表示,vis[21][21][1<<14]中 //前两维表示蛇头的位置,后一维中的二进制中每两位表示蛇身体相对于 //前一节身体的方向,正好0,1,2,3四个数表示四个方向。这样就可以把蛇的 //状态标识出来了。 //代码略挫,清楚思路就好。 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int n,m,l,k,mp[25][25]; int dir[3][3]={-1,1,-1,3,-1,2,-1,0,-1}; int D[4][2]={1,0,-1,0,0,1,0,-1}; bool vis[21][21][1<<14]; struct Body{ int x[10],y[10]; int cnt; }body,bb; bool chak(int a,int b,Body c){ for(int i=2;i<=l;i++){ if(a==c.x[i]&&b==c.y[i]) return true; } return false; } int bfs(){ queue<Body>q; body.cnt=0; q.push(body); while(!q.empty()){ body=q.front();q.pop(); if(body.x[1]==1&&body.y[1]==1) return body.cnt; for(int i=0;i<4;i++){ int x=body.x[1]+D[i][0],y=body.y[1]+D[i][1]; if(x<1||x>n||y<1||y>m||mp[x][y]) continue; if(chak(x,y,body)) continue; body.x[0]=x;body.y[0]=y; int sta=0,tmp1,tmp2; for(int j=l;j>=1;j--){ bb.x[j]=body.x[j-1];bb.y[j]=body.y[j-1]; if(j!=l){ tmp1=bb.x[j]-bb.x[j+1]+1; tmp2=bb.y[j]-bb.y[j+1]+1; int tmp=dir[tmp1][tmp2];tmp=(tmp<<(14-j*2)); if(dir[tmp1][tmp2]!=-1) sta=(sta|tmp); } } if(vis[x][y][sta]) continue; vis[x][y][sta]=1; bb.cnt=body.cnt+1; q.push(bb); } } return -1; } int main() { int cas=0,x,y; while(scanf("%d%d%d",&n,&m,&l)&&n&&m&&l){ memset(mp,0,sizeof(mp)); memset(vis,0,sizeof(vis)); int sta=0; scanf("%d%d",&body.x[1],&body.y[1]); for(int i=2;i<=l;i++){ scanf("%d%d",&body.x[i],&body.y[i]); x=body.x[i-1]-body.x[i]+1; y=body.y[i-1]-body.y[i]+1; int tmp=dir[x][y];tmp=(tmp<<(14-(i-1)*2)); if(dir[x][y]!=-1) sta=(sta|tmp); } vis[body.x[1]][body.y[1]][sta]=1; scanf("%d",&k); for(int i=0;i<k;i++){ scanf("%d%d",&x,&y); mp[x][y]=1; } printf("Case %d: %d ",++cas,bfs()); } return 0; }