Holedox Moving POJ - 1324 (bfs + 状态压缩+A*)

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. 

题意: 输入m,n,l,m行n列,l为蛇身的长度,紧接着l行为 整个蛇的位置,输入p,下面p行为为障碍物的位置;求最少几步在 不撞上蛇身和障碍物时,最少几步到达 (1,1)点,我觉着这题没有说清楚,每走一格,蛇尾也在动,而给你的数据时9步,我觉3步就行了,输出的9步就按9步的写吧;

思路:这道题就是 就是状态不好标记;所以这里用到了 状态压缩, 怎么压缩呢? 从蛇头到蛇身,记录后一个与前一个的相对位置; 因为 4个方向,0,1,2,3, 最大为3,二进制 11,蛇身长度最长为8,所以我们标记最多只需 14位二进制,标记状态就行了,最后两位二进制,表示 蛇身第二个格相于蛇头的位置,倒数3,4 位二进制表示 蛇身第三个相对于 蛇身第二个的位置,依次类推, 在bfs时,可以用 A* 减值,a.step + a.d 表示当前走的步数 加上 到终点的最小步数,最小;

注意提交时,要用G++,c++不过;

有这一道题发现两种 写优先队列的方法,bool operator<(node a) const 这种比较快点;

代码:

  

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define Max 21
#include<queue>
int n,m,l;

int mod; 
int a[4][2] = {0,-1,1,0,0,1,-1,0};

int vis[Max][Max][1<<14];
int str[Max][Max];
int sy,sx;
int f1[8],f2[8];
struct node
{
	int x,y;
	int val; // 状态值; 
	int step;
	int d;
	int f1[8];
	int f2[8];
	/*friend bool operator < (node a,node b)  // A*  当前深度 + 到目标的最短的距离; 
	{
		return a.step + a.d > b.step + b.d;
	}*/
	bool operator <(node a) const
	{
		return a.step+a.d < step+d;
	}
};

int fff(int y,int x,int y1,int x1)
{
	if(x>x1)  return 3;
	else if(x<x1) return 1;
	else if(y>y1) return 0;
	else if(y<y1) return 2;
}
int bfs(int k)
{
	node tt;
	tt.x = sx;
	tt.y = sy;
	tt.val = k;
	tt.step = 0;
	for(int i = 0;i<l;i++)
	{
		tt.f1[i] = f1[i]-1;
		tt.f2[i] = f2[i]-1;
	}
	tt.d = sy + sx;
	vis[sy][sx][k] = 1;
	if(sy==0&&sx ==0) return tt.step;
	priority_queue<node >q;
	q.push(tt);
	while(!q.empty())
	{
		node star = q.top();
		q.pop();
		
		for(int i = 0;i<4;i++)
		{
			int tx = star.x + a[i][0];
			int ty = star.y + a[i][1];
			if(tx>=0&&ty>=0&&tx<n&&ty<m&&!str[ty][tx])
			{
				
				node end;
				int p = star.val;
				p = p<<2;
				p %= mod;
				
				int t = fff(ty,tx,star.y,star.x);
				p |= t;
				
				if(!vis[ty][tx][p])
				{
					int j;
					for(j = 0;j<l;j++)
					{
						if(ty==star.f1[j]&&tx==star.f2[j])
							break;
						end.f1[j+1] = star.f1[j];
						end.f2[j+1] = star.f2[j];
					}
					if(j<l) continue;
					end.f1[0] = ty;
					end.f2[0] = tx;
					vis[ty][tx][p] = 1;
					end.x = tx;
					end.y  = ty;
					end.step = star.step + 1;
					end.val = p;
					end.d = ty + tx;
					if(ty==0&&tx==0)
						return end.step;
					q.push(end);
				}				
			}
		}
	}
	return -1;
}

int main()
{
	int i,j;
	int num = 1;
	while(~scanf("%d%d%d",&m,&n,&l)&&(m||n||l))
	{
		scanf("%d%d",&f1[0],&f2[0]);
		memset(vis,0,sizeof(vis));
		memset(str,0,sizeof(str));
		int res = 0;
		 mod = 1<<(2*(l-1));
		for(i = 1;i<l;i++)
		{
			scanf("%d%d",&f1[i],&f2[i]);
			int k = fff(f1[i-1],f2[i-1],f1[i],f2[i]);
			res = res|(k<<(2*(i-1)));
		}
		int k ;
		scanf("%d",&k);
		while(k--)
		{
			int x,y;
			scanf("%d%d",&y,&x);
			str[y-1][x-1] = 1;
		}
		sy = f1[0] - 1;
		sx = f2[0] - 1;
		printf("Case %d: %d\n",num++,bfs(res));
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值