POJ 1324 Holedox Moving

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 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


用一个结构体存储贪吃蛇和走的步数。然后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;
		else
			return 1;
	}
	else
	{
		if(nx>px)
			return 2;
		else
			return 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值