UVALive 3956 Key Task (bfs+状态压缩)

Key Task

题目链接:

http://acm.hust.edu.cn/vjudge/contest/129733#problem/D

Description

The Czech Technical University is rather old -- you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places. The result is that some first-graders have often diffculties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game. The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four different types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color. You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.

Input

The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 <= R, C <= 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following: ``` Character Meaning Hash mark # Wall Dot . Free square Asterisk * Your position Uppercase letter B Y R G Blue, yellow, red, or green door Lowercase letter b y r g Blue, yellow, red, or green key Uppercase X X Exit ``` Note that it is allowed to have more than one exit, no exit at all, more doors and/or keys of the same color, and keys without corresponding doors and vice versa. You may assume that the marker of your position ("*") will appear exactly once in every map. There is one blank line after each map. The input is terminated by two zeros in place of the map size.

Output

For each map, print one line containing the sentence "Escape possible in S steps.", where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string "The poor student is trapped!" instead. One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.

Sample Input

``` 1 10 *........X

1 3
*#X

3 20
####################

XY.gBr.*.Rb.G.GG.y#

####################

0 0

</big>

##Sample Output
<big>

Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps.

</big>


##Source
<big>
2016-HUST-线下组队赛-2
</big>




<br/>
##题意:
<big>
在n*m的地图上有一个起点和若干终点,有4种颜色的门及其对应钥匙,求最少的时间从起点到达任一终点.
</big>


<br/>
##题解:
<big>
类似魔塔的搜索题,这里用bfs来搜最小步数即可. 用状态压缩处理每个位置拿到的钥匙状态.
判重:除了点坐标外,需要额外记录达到当前点的钥匙状态. (因为有些位置可能重复到达).
</big>




<br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n, m;
char mp[maxn][maxn];
bool vis[maxn][maxn][1<<4];

bool is_ok(int x, int y) {
    return x>=0 && y>=0 && x<n && y<m;
}

struct node {
    int x,y;
    int step, key;
};
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

int bfs(int sx, int sy) {
    queue<node> q;
    while(!q.empty()) q.pop();
    node cur, next;
    cur.x = sx, cur.y = sy, cur.step = 0, cur.key = 0;
    q.push(cur);
    vis[sx][sy][0] = 1;

    while(!q.empty()) {
        cur = q.front(); q.pop();
        for(int i=0; i<4; i++) {
            int xx = cur.x + dir[i][0];
            int yy = cur.y + dir[i][1];
            if(!is_ok(xx,yy) || mp[xx][yy]=='#') continue;
            next.x = xx, next.y = yy, next.step = cur.step + 1; next.key = cur.key;

            if(mp[xx][yy] == 'X') return next.step;

            if(mp[xx][yy]>='a' && mp[xx][yy]<='z') {
                if(mp[xx][yy] == 'b') next.key |= (1<<0);
                if(mp[xx][yy] == 'y') next.key |= (1<<1);
                if(mp[xx][yy] == 'r') next.key |= (1<<2);
                if(mp[xx][yy] == 'g') next.key |= (1<<3);
            }

            if(mp[xx][yy]>='A' && mp[xx][yy]<='Z') {
                if( (mp[xx][yy] == 'B' && !(next.key&(1<<0))) ||
                    (mp[xx][yy] == 'Y' && !(next.key&(1<<1))) ||
                    (mp[xx][yy] == 'R' && !(next.key&(1<<2))) ||
                    (mp[xx][yy] == 'G' && !(next.key&(1<<3))) )
                continue;
            }

            if(vis[next.x][next.y][next.key]) continue;
            vis[next.x][next.y][next.key] = 1;
            q.push(next);
        }
    }

    return -1;
}

int main(int argc, char const *argv[])
{
    //IN;

    while(scanf("%d %d", &n,&m) != EOF && (n||m))
    {
        for(int i=0; i<n; i++)
            scanf("%s", mp[i]);

        int sx = -1, sy = -1;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) if(mp[i][j] == '*') {
                sx = i, sy = j; break;
            }
            if(sx != -1) break;
        }

        memset(vis, 0, sizeof(vis));
        int ans = bfs(sx, sy);

        if(ans == -1) printf("The poor student is trapped!\n");
        else printf("Escape possible in %d steps.\n", ans);
    }

    return 0;
}
posted @ 2016-08-23 13:48  Sunshine_tcf  阅读(364)  评论(0编辑  收藏  举报