Holedox Moving
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 12014 |
|
Accepted: 2882 |
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.
题意:就不说了 大家都懂的
思路:主要是要用状态压缩的方法存蛇的状态 用一个三维数组 vis[x][y][state] 前两维表示蛇头坐标 后一维存用二进制的方法存蛇尾的状态 用两位二进制表示方向 将上下左右分别看为 00 11 01 10
如图一 :则表示为vis[4][1][18] 18的后六位二进制是 010010 从后往前看 表示蛇头向右得到第一个蛇身 然后第一个蛇身向上得到第二个蛇身,依次类推,可以将整个蛇表示出来
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define maxn 21
using namespace std;
int n,m,l,k,ans,cnt,hehe;
int prex,prey;
int mp[maxn][maxn];
int vis[maxn][maxn][1<<14];
int dx[4]={-1,1,0,0}; // up down left right
int dy[4]={0,0,-1,1};
int undx[4]={-1,0,0,1};
int undy[4]={0,-1,1,0};
int dir[]={0,3,1,2}; // 00 11 01 10
int undir[]={3,0,2,1};
int val[8]={1,4,16,64,256,1024,4096,16384};
struct snake
{
int x,y;
int cnt,state; // 步数、状态
}curs,nows;
queue<snake> q;
int cxx=0;
bool isok(int u,int v,int p) // 判断是否能走
{
int i,j,x2,y2,x3,y3,tst,tmp,tst1;
x2=u;
y2=v;
tst=nows.state;
tst1=(tst<<2)%hehe;
tst1=tst1+undir[p];
if(x2>n||x2<1||y2>m||y2<1||mp[x2][y2]||vis[x2][y2][tst1]) return false; //判断是否走过或出界或撞墙
x3=u-dx[p];
y3=v-dy[p];
for(i=1;i<=l;i++) // 判断是否撞蛇尾
{
if(x2==x3&&y2==y3) return false;
tmp=tst%4; // 计算蛇尾则可以依次取方向 由前往后推
tst=tst/4;
x3+=undx[tmp];
y3+=undy[tmp];
}
return true;
}
bool bfs()
{
int i,j,xx,yy,nx,ny;
int st,curst,tempst,tempcnt;
while(!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
vis[curs.x][curs.y][curs.state]=1;
q.push(curs);
while(!q.empty())
{
cxx++;
nows=q.front();
xx=nows.x;
yy=nows.y;
st=nows.state;
tempcnt=nows.cnt;
if(xx==1&&yy==1)
{
ans=nows.cnt;
return true;
}
for(i=0;i<4;i++)
{
// printf("dir[%d]:%d st:%d\n",i,dir[i],st);
if(dir[i]==st%4) continue ;
nx=xx+dx[i];
ny=yy+dy[i];
if(isok(nx,ny,i))
{
curs.x=nx;
curs.y=ny;
curs.cnt=tempcnt+1;
tempst=(st<<2)%hehe; // 蛇移动则可这样计算 向左移两位取模
curst=tempst+undir[i]; // 加上移动的方向
curs.state=curst;
q.push(curs);
vis[nx][ny][curst]=1;
}
}
q.pop();
}
return false;
}
int main()
{
int i,j,temp,x1,y1,xxc=0;
while(scanf("%d%d%d",&n,&m,&l),n||m||l)
{
xxc++;
scanf("%d%d",&curs.x,&curs.y);
prex=curs.x;
prey=curs.y;
temp=0;
hehe=1<<(2*l-2);
for(i=0;i<l-1;i++) // 输入时把蛇的状态存下来
{
scanf("%d%d",&x1,&y1);
for(j=0;j<4;j++)
{
if(prex+dx[j]==x1&&prey+dy[j]==y1) break;
}
prex=x1;
prey=y1;
temp+=dir[j]*val[i];
}
curs.state=temp;
curs.cnt=0;
memset(mp,0,sizeof(mp));
scanf("%d",&k);
for(i=1;i<=k;i++)
{
scanf("%d%d",&x1,&y1);
mp[x1][y1]=1;
}
printf("Case %d: ",xxc);
if(bfs()) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}