POJ-1324 Holedox Moving

Holedox Moving
Time Limit: 5000MS   Memory Limit: 65536K
 

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).
POJ-1324 Holedox Moving_第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.

————————————————————集训4.1的分割线————————————————————

前言:这代码足足写了一天啊。。。而且是在听了瓜神的指导,知道了判重方法的情况下。

思路:很容易看出这是一道BFS题,但是并不容易想出判重的方法。Hash整张图是不可能的,因为20×20的图,完全散列就是20! = 2.4 * 10 18张图了。根本不能忍啊。

但是仔细观察,发现贪吃蛇爬行过程中,每一次只有头部和尾部的位置发生变化。这就意味着蛇的绝大部分相对位置不会变化(尽管绝对位置都会变化)。

因此只需要记录蛇头的坐标以及相对前一个点的位置就行了。L <= 8,要Hash这条蛇很简单。想得到的话可以用一个int的二进制来表示相对上一点的位置,想不到的话用8个int表示也还行。

if(!vis[r][c][S]...)...

然后难点是撞到自己的身上。尾部那个点是可以走的,因为头移动到尾上,尾就已经离开了。其余身体都是不能走的点,根据头部坐标依次求出其他点的绝对坐标,和新的头部判断一下即可。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
/****************************************/
//00 上  = 0, 01 右  = 1, 10 左  = 2, 11 下  = 3
#define LIM (n_hr >= 0 && n_hr < n && n_hc >= 0 && n_hc < m)
const int N = 25, S = 1 << 15;
const int d[][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}};
int n, m, L, obs, r, c;
struct Snake
{
	int head_r, head_c;
	int body, step;
};
Snake s;
bool mat[N][N], vis[N][N][S];
queue  q;

bool suicide(int dir, int sb, Snake &son, int fr, int fc)
{
	int &hr = son.head_r, &hc = son.head_c;
	int i = (L-2)*2;//取头部后面点的位置需要左移的位数
	son.body = (sb>>2) + ((3-dir) << i);//新的蛇
	for(; i >= 0; i -= 2) {
		int k = (sb >> i) & 3;//取相对位置
		fr += d[k][0];
		fc += d[k][1];
		if(hr == fr && hc == fc) return true;//自杀了
	}
	return false;
}

int bfs(Snake st)
{
	q.push(st);
	Snake t, nt;
	while(!q.empty()) {
		t = q.front();
		q.pop();
		for(int dd = 0; dd < 4; dd++) {
			int &n_hr = nt.head_r, &n_hc = nt.head_c;
			n_hr = t.head_r + d[dd][0];
			n_hc = t.head_c + d[dd][1];
			if(!LIM || mat[n_hr][n_hc]) continue;//短路运算符 不会RE
			if(suicide(dd, t.body, nt, t.head_r, t.head_c))
				continue;
			if(!vis[n_hr][n_hc][nt.body]) {//判重
				vis[n_hr][n_hc][nt.body] = true;
				nt.step = t.step + 1;
				if(n_hr == 0 && n_hc == 0)
					return nt.step;
				q.push(nt);
			}
		}
	}
	return -1;
}

void read_S()
{
	int last_r, last_c;
	for(int i = 0; i < L; i++) {
		scanf("%d%d", &r, &c);
		r--; c--;
		if(i == 0) {
			last_r = s.head_r = r;
			last_c = s.head_c = c;
			s.body = 0;
		}
		else {//记录相对位置,压缩到一个int里
			if(c == last_c) {
				if(r < last_r)
					s.body = (s.body<<2) + 0;
				else
					s.body = (s.body<<2) + 3;
			}
			else {
				if(c > last_c)
					s.body = (s.body<<2) + 1;
				else
					s.body = (s.body<<2) + 2;
			}
			last_r = r; last_c = c;
		}
	}
}

int main()
{
	int cas = 1;
	while(~scanf("%d%d%d", &n, &m, &L), n||m||L) {
		memset(mat, 0, sizeof(mat));
		memset(vis, 0, sizeof(vis));
		while(!q.empty()) q.pop();//将前一个cas的队列清空
		read_S();//读蛇
		vis[s.head_r][s.head_c][s.body] = true;
		scanf("%d", &obs);
		for(int i = 0; i < obs; i++) {
			scanf("%d%d", &r, &c);
			r--; c--;
			mat[r][c] = true;
		}//读图
		s.step = 0;
		if(s.head_r == 0 && s.head_c == 0)
			printf("Case %d: 0\n", cas++);//一开始就在终点
		else
			printf("Case %d: %d\n", cas++, bfs(s));
	}
	return 0;
}


你可能感兴趣的:(哈希,ACM-POJ,BFS)