poj1324 Holedox Moving

来源:互联网 发布:付费语音问答源码 编辑:程序博客网 时间:2024/06/12 19:08

Holedox Moving
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 12926 Accepted: 3055

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


参考:http://www.cnblogs.com/kedebug/archive/2013/03/26/2983143.html

大致题意: 给定一个地图和一条蛇,问蛇需要最少走多少步才能走到出口(1,1),如果无法到达则-1
大体思路: 因为蛇最多有7个身体那么每个身体都有上下左右4种状态(00-11二进制),可以用2^(7*2) = 1<<14表示所有的状态
  后一个身体的状态可以根据前一个身体决定, 有vis[x][y][state] 表示蛇头在(x,y)是身体的状态(是否出现过)。
采用bfs处理最小,最大的步数(没有蛇身和蛇身不动),最后采用astar找到最大步数。

#include <stdio.h>#include <string.h>#include <queue>#include <math.h>using namespace std;char vis[21][21][1<<14];char stone[21][21];int row, col, l, k;int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};struct Body{int x, y;}body[10];struct ST{int x, y, step, state, f;ST(int _x, int _y, int _step, int _state, int _f){x = _x; y = _y;step = _step; state = _state; f = _f;}bool operator<(const ST& a) const{return f > a.f;}};void setstone(){for (int i=1; i<l; ++i){stone[body[i].x][body[i].y] = 1;}}void clearstone(){for (int i=1; i<l; ++i){stone[body[i].x][body[i].y] = 0;}}int getstate(int x, int y){int state = 0;for (int i=1; i<l; ++i){state <<= 2;if (body[i].x > body[i-1].x) // up{state |= 0;}else if (body[i].x < body[i-1].x) // downstate |= 1;else if (body[i].y > body[i-1].y) // leftstate |= 2;else if (body[i].y < body[i-1].y)state |= 3;}return state;}void setbody(int x, int y, int state){body[0].x = x; body[0].y = y;for (int i=1; i<l; ++i){int val = (state >> ((l-i-1)*2)) & 3;body[i].x = body[i-1].x;body[i].y = body[i-1].y;if (val == 0)body[i].x += 1;else if (val == 1)body[i].x -= 1;else if (val == 2)body[i].y += 1;else if (val == 3)body[i].y -= 1;}}bool judge(int x, int y){if ( 0 < x && x <=row && 0<y && y<=col && !stone[x][y])return true;return false;}int abs(int x){return x>0?x:-x;}inline int getdiff(int x, int y){return abs(x-1) + abs(y-1);}int astar(){priority_queue<ST> q;int x = body[0].x, y = body[0].y;int state = getstate(x, y);int f = getdiff(x, y);q.push(ST(x, y, 0, state, f));vis[x][y][state] = 1;while (!q.empty()){ST u = q.top();q.pop();if (u.x == 1 && u.y == 1)return u.step;setbody(u.x, u.y, u.state);setstone();for (int i=0; i<4; ++i){x = u.x + dir[i][0];y = u.y + dir[i][1];state = (l<2)?0:(u.state>>2) | (i<<((l-2)*2));if (judge(x,y) && !vis[x][y][state]){vis[x][y][state] = true;f = u.step + 1 + getdiff(x, y);q.push(ST(x, y, u.step+1, state, f));}}clearstone();}return -1;}int bfs(){char vis1[21][21] = {0};priority_queue<ST> q;int x = body[0].x, y = body[0].y;int f = getdiff(x, y);q.push(ST(x, y, 0, 0, f));vis1[x][y] = 1;while (!q.empty()){ST u = q.top();q.pop();if (u.x == 1 && u.y == 1)return u.step;for (int i=0; i<4; ++i){x = u.x + dir[i][0];y = u.y + dir[i][1];if (judge(x, y) && !vis1[x][y]){vis1[x][y] = 1;f = u.step + 1 + getdiff(x, y);q.push(ST(x, y, u.step + 1, 0, f));}}}return -1;}int main(){int cases = 0;while (scanf("%d%d%d", &row, &col, &l), row+col+l){for (int i=0; i<l; ++i){scanf("%d%d", &body[i].x, &body[i].y);}memset(stone, 0, sizeof(stone));memset(vis, 0, sizeof(vis));scanf("%d", &k);for (int i=0; i<k; ++i){int x, y;scanf("%d%d", &x, &y);stone[x][y] = 1;}int ans;int minstep = bfs();setstone();int maxstep = bfs();clearstone();if (minstep == -1)ans = -1;else if (minstep == maxstep)ans = minstep;elseans = astar();printf("Case %d: %d\n", ++cases, ans);}return 0;}


0 0
原创粉丝点击