poj 1324 Holedox Moving(BFS+位运算状压判重)

Holedox Moving
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12871 Accepted: 3040

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



题目大意:贪吃蛇的简化,已知一些障碍,求蛇到达出口(1, 1)的最短步数,不能到达输出-1。
解题思路:毫无疑问是BFS搜索,难点在于记录蛇的状态判重。因为蛇是不断的移动,我们可以用0, 1, 2, 3记录当前节点相当于前驱结点的方向,蛇最长有七节,也就是4^7中状态,用一个三维数组vis[x][y][4^7]来判重,其中x, y表示蛇的头结点,用mp[x][y]判断是否可以行走,每次更新。
还有一点注意的是队列不能用STL,会RE,,手拍一个queue, 2407MS  AC。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 25;
const int M = 16400;
const int R = 10;

struct Point{
    int x[R], y[R];
    int step;
    int dir[R];
}que[50010];

int n, m, l;
int vis[N][N][M];
int mp[N][N];
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
int front, rear;

int pre_dir(int px, int py, int x, int y){ // 得到前一个节点的方向
    if(py > y) return 0;
    else if(px > x) return 1;
    else if(py < y) return 2;
    else return 3;
}

int calhash(Point x){  // 状态压缩
    int sum = 0;
    for(int i = 1; i < l; i++)
        sum = sum * 4 + x.dir[i];
    return sum;
}

int bfs(){
    Point fr, tmp;
    while(front != rear){
        front = (front + 1) % 50000;
        fr = que[front];
        if(fr.x[0] == 1 && fr.y[0] == 1){
            return fr.step;
        }
        for(int i = 1; i < l; i++)
            mp[fr.x[i]][fr.y[i]] = -1;
        for(int i = 0; i < 4; i++){
            int xx = fr.x[0] + dir[i][0];
            int yy = fr.y[0] + dir[i][1];
          //  cout << xx << " " << yy << " " << mp[xx][yy] << endl;
            if(xx > 0 && xx <= n && yy > 0 && yy <= m && mp[xx][yy] == 0){
                tmp.x[0] = xx;
                tmp.y[0] = yy;
                tmp.step = fr.step + 1;
                tmp.dir[1] = pre_dir(xx, yy, fr.x[0], fr.y[0]);
                for(int j = 1; j < l; j++){
                    tmp.x[j] = fr.x[j - 1];
                    tmp.y[j] = fr.y[j - 1];
                    if(j != 1) tmp.dir[j] = fr.dir[j - 1];
                }
                int kk = calhash(tmp);
                if(!vis[xx][yy][kk]){
                    vis[xx][yy][kk] = 1;
                    rear = (rear + 1) % 50000;
                    que[rear] = tmp;
                }
            }
        }
        for(int i = 1; i < l; i++)
            mp[fr.x[i]][fr.y[i]] = 0;
    }
    return -1;
}

int main(){
    Point start;
    int cas = 1;
    while(scanf("%d%d%d", &n, &m, &l) != EOF , n || m || l){
        memset(vis, 0, sizeof(vis));
        memset(mp, 0, sizeof(mp));
        for(int i = 0; i < l; i++){
            scanf("%d%d", &start.x[i], &start.y[i]);
            if(i > 0) start.dir[i] = pre_dir(start.x[i - 1], start.y[i - 1], start.x[i], start.y[i]);
        }
        int k, u, v;
        scanf("%d", &k);
        for(int i = 0; i < k; i++){
            scanf("%d%d", &u, &v);
            mp[u][v] = -1;
        }
        front = -1;
        rear = 0;
        start.step = 0;
        que[0] = start;
        vis[start.x[0]][start.y[0]][calhash(start)] = 1;
        printf("Case %d: %d\n", cas++, bfs());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值