搜索. Poj1324. Holedox Moving.

来源:互联网 发布:剑灵力士卡刀软件 编辑:程序博客网 时间:2024/05/29 02:19

http://poj.org/problem?id=1324

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 44 14 23 23 132 33 33 44 4 42 31 31 42 442 12 23 44 20 0 0

Sample Output

Case 1: 9Case 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. 


题目大意: 在一个n*m的矩阵中, 存在一条蛇. 现在的目的是使蛇头到达(1, 1). 规则和贪食蛇差不多, 不允许走进当前身体部分占用的格子. (包括蛇尾... 样例就证明了这一点.) 有一些障碍格子不能走, 问可能的最小步数.

分析: 大致上看, 数据范围比较小. 应该用搜索. 面对搜索题, 特别是这种矩阵行走的, 要特别考虑一下判重. 这道题目的判重比较容易实现, 当确定了蛇头的位置, 下一个身体部分必然会与蛇头相邻, 也就是最多有4种选择. 所以状态数最多为 n * m * 4 ^ (L - 1). (达不到. 但这个在可接受的范围内, 所以就以这个分析. )

那么剩下的就比较简单了. 最简单的想法是 直接搜, 也能过... 但也可以用A*. 至于对身体占用的格子判重, 还有对身体占用的位置进行编码, 可以使用位运算, 或者直接像我第一个程序那样用数组记录也可以.. 
要特别注意蛇头一开始就在 (1, 1) 的情况.
直接搜:
#include <fstream>#include <algorithm>#include <iostream>using namespace std;const int xx[] = {1, -1, 0, 0};const int yy[] = {0, 0, 1, -1};const int maxs = 27214400 + 10;const int maxn = 20 + 10;int n, m, L;int x[maxn], y[maxn];bool map[maxn][maxn], vis[maxs];bool Init() {int k, _x, _y;for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) map[i][j] = false;for (int i = 0; i < maxs; i++) vis[i] = false;scanf("%d%d%d", &n, &m, &L);if (n == 0 && m == 0 && L == 0) return false;for (int i = L - 1; i > -1; i--) {scanf("%d%d", &x[i], &y[i]);x[i]--;y[i]--;}scanf("%d", &k);for (int i = 0; i < k; i++) {scanf("%d%d", &_x, &_y);map[_x - 1][_y - 1] = true;}return true;}int code(int *x, int *y) {int rt = x[L - 1], p = 400;rt += y[L - 1] * 20;for (int i = L - 2; i > -1; i--) {if (x[i] - x[i + 1] == 1) {rt += p * 1;}if (y[i] - y[i + 1] == -1) {rt += p * 2;}if (y[i] - y[i + 1] == 1) {rt += p * 3;}p *= 4;}return rt;}void decode(int c, int *x, int *y) {x[L - 1] = c % 20;c /= 20;y[L - 1] = c % 20;c /= 20;for (int i = L - 2; i > -1; i--) {int tmp = c % 4;if (tmp == 0) {x[i] = x[i + 1] - 1;y[i] = y[i + 1];}if (tmp == 1) {x[i] = x[i + 1] + 1;y[i] = y[i + 1];}if (tmp == 2) {x[i] = x[i + 1];y[i] = y[i + 1] - 1;}if (tmp == 3) {x[i] = x[i + 1];y[i] = y[i + 1] + 1;}c /= 4;}}int ans;int o[maxs], step[maxs];int bfs() {if (x[L - 1] == 0 && y[L - 1] == 0) return 0;int h = 0, t = 0;o[h] = code(x, y);step[h] = 0;while (h <= t) {decode(o[h], x, y);int tmpx = x[L - 1], tmpy = y[L - 1]; for (int i = 0; i < 4; i++) {int x1 = tmpx + xx[i];int y1 = tmpy + yy[i];if (x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || map[x1][y1]) continue;bool flag = true;for (int j = 0; j < L - 1; j++)if (x1 == x[j] && y1 == y[j]) {flag = false;break;}if (!flag) continue;x[L] = x1;y[L] = y1;int c = code(x + 1, y + 1);if (vis[c]) continue;t++;o[t] = c;vis[c] = true;step[t] = step[h] + 1;if (x1 == 0 && y1 == 0) {return step[t];}}h++;}return -1;}int times;void Solve() {ans = maxs + 1;times++;printf("Case %d: ", times);printf("%d\n", bfs());}int main() {freopen("prog.in", "r", stdin);freopen("prog.out", "w", stdout);while (Init()) Solve();return 0;}

A*:
#include <fstream>#include <iostream>#include <queue>#include <map>#include <string.h>using namespace std;const int maxn = 20 + 2;const int xx[] = {-1, 0, 0, 1};const int yy[] = {0, -1, 1, 0};int n, m, l, origin, mask, stx, sty, times, len;int mat[maxn][maxn], o[maxn * maxn];int comx[maxn], comy[maxn];bool stone[maxn][maxn];struct Snake {int state, s;Snake (int a = 0, int b = 0) : state(a), s(b) {}bool operator < (const Snake &tar) const{return (s + mat[ ((state >> len) >> 5) ][ (state >> len) & 31 ]) > (tar.s + mat[ ((tar.state >> len) >> 5) ][ (tar.state >> len) & 31] );}};void inputSnake() {int px, py, x, y, i, k;for (i = 0; i < maxn; i++)for (k = 0; k < maxn; k++) stone[i][k] = false;origin = 0;for (i = 0; i < l; i++) {origin <<= 2;scanf("%d%d", &x, &y);x--;y--;if (i != 0) {if (x - px == 1) origin |= 3;else if (x - px == -1) origin |= 0;else if (y - py == 1) origin |= 2;else if (y - py == -1) origin |= 1;}else {origin |= (x << 5) | y; stx = x;sty = y;}px = x;py = y;}scanf("%d", &k);for (i = 0; i < k; i++) {scanf("%d%d", &x, &y);stone[x - 1][y - 1] = true;}l--;len = l << 1;}void search() {int h = 0, t = 0, i, s, cx, cy, step, x, y;for (i = 0; i < n; i++)for (s = 0; s < m; s++) mat[i][s] = 100000000;mat[0][0] = 0;o[h] = 0;while (h <= t) {s = o[h++];cx = s & 31;cy = (s >> 5) & 31;step = (s >> 10) + 1;for (i = 0; i < 4; i++) {x = cx + xx[i];y = cy + yy[i];if (x >= 0 && y >= 0 && x < n && y < m && !stone[x][y] && step < mat[x][y]) {mat[x][y] = step;o[++t] = x | (y << 5) | (step << 10);}}}}int tot = 0;int bfs() {priority_queue <Snake> list;map <int, bool> hash;int i, j, x, y, px, py, cx, cy, s, tmp, c;Snake cur;list.push( Snake(origin, 0) );hash[ origin ] = true;while (!list.empty()) {cur = list.top();s = cur.state;if (!(s >> len)) return cur.s;cx = (s >> len) >> 5;cy = (s >> len) & 31;list.pop();px = cx;py = cy;for (i = l - 1; i > -1; i--) {tmp = (s >> (i << 1)) & 3;px += xx[tmp];py += yy[tmp];comx[i] = px;comy[i] = py;}for (i = 0; i < 4; i++) {x = cx + xx[i];y = cy + yy[i];if (x < 0 || y < 0 || x > n - 1 || y > m - 1 || stone[x][y]) continue;px = cx, py = cy;for (j = l - 1; j > -1; j--) {if (comx[j] == x && comy[j] == y) break;}if (j != -1) continue;if (!(x | y)) return cur.s + 1;c = ((s >> 2) & (((1 << (len - 2)) - 1))) | ( (3 << (len - 2)) & ((3 - i) << (len - 2)) ) | ((x << 5) << len) | (y << len);if (!hash.count(c)) {hash[c] = true;list.push( Snake(c, cur.s + 1) );}}}return -1;}int main() {while (scanf("%d%d%d", &n, &m, &l) && (n | m | l)) {inputSnake();search();if (mat[stx][sty] > 400) {printf("Case %d: -1\n", ++times);continue;}bfs();printf("Case %d: %d\n", ++times, bfs());}return 0;}
有点奇怪的是. 当我用A*的程序, 删掉
if (mat[stx][sty] > 400) {printf("Case %d: -1\n", ++times);continue;}
提交, 竟然比我直接搜更慢... 3000+ms... +上之后就有188ms....
然后在传说数据较强的SGU上提交, A*那个都能排进第一页.... 20Ms...

0 0
原创粉丝点击