题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1885

Key Task

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 di?culties 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 di?erent 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 \leq R,\ C \leq 100)$ specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:

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

Sample Output

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

状压bfs,每个格子16种状态,用三维数组标记即可。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
char G[N][N];
bool vis[N][N][];
int H, W, Sx, Sy;
const int dx[] = { , , -, }, dy[] = { -, , , };
struct Node {
int x, y, key, s;
Node() {}
Node(int i, int j, int k, int l) :x(i), y(j), key(k), s(l) {}
};
inline int work(char ch) {
if (ch == 'B' || ch == 'b') return ;
if (ch == 'Y' || ch == 'y') return ;
if (ch == 'R' || ch == 'r') return ;
if (ch == 'G' || ch == 'g') return ;
return ;
}
void bfs() {
bool f = true;
cls(vis, false);
queue<Node> q;
q.push(Node(Sx, Sy, , ));
vis[Sx][Sy][] = true;
while (!q.empty()) {
Node t = q.front(); q.pop();
if (G[t.x][t.y] == 'X') {
printf("Escape possible in %d steps.\n", t.s);
return;
}
rep(i, ) {
int key = t.key, x = t.x + dx[i], y = t.y + dy[i];
if (x < || x >= H || y < || y >= W) continue;
if (vis[x][y][key] || G[x][y] == '#') continue;
if (isupper(G[x][y]) && G[x][y] != 'X' && !(key &( << work(G[x][y])))) continue;
if (islower(G[x][y])) key |= << work(G[x][y]);
q.push(Node(x, y, key, t.s + ));
vis[x][y][key] = true;
}
}
puts("The poor student is trapped!");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%d %d", &H, &W), H + W) {
rep(i, H) {
scanf("%s", G[i]);
rep(j, W) {
if (G[i][j] == '*') Sx = i, Sy = j;
}
}
bfs();
}
return ;
}

hdu 1885 Key Task的更多相关文章

  1. HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others)  ...

  2. HDU 1885 Key Task(三维BFS)

    题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...

  3. HDU 1885 Key Task 国家压缩+搜索

    点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 1885 Key Task(bfs+位运算)

    题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...

  5. hdu 1885 Key Task (三维bfs)

    题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候  有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...

  6. hdu 1885 Key Task(bfs+状态压缩)

    Problem Description The Czech Technical University years of its existence . Some of the university b ...

  7. hdu 1885 Key Task(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...

  8. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  9. 【HDOJ】1885 Key Task

    状态压缩+BFS,一次AC. /* 1885 */ #include <iostream> #include <queue> #include <cstring> ...

随机推荐

  1. Flash Builder 4.6 BUG 远程访问受阻

    今天调试项目的时候,惊讶的发现在使用RemoteObject进行远程访问时出现奇怪现象,只能在服务器本地实现访问,在其他客户机上提示2048错误,send failed,差点没把我吓死,记得之前测试过 ...

  2. 学习总结 java 父子级

    package com.hanqi; //父类 public class Father { // public Father() // { // // } // public Father(Strin ...

  3. mysql下的常用操作

    本文继 linux下安装mysql,记录下在工作中最常用的mysql语句 MySQL添加字段和删除字段 添加字段: alter table `user_movement_log`Add column ...

  4. IBM Tivoli NetView网管软件实战

    广大网络管理者需要一款包括网络监控故障处理的一个集成网络管理平台,本文根据作者多年实践提出的基于IBM Tivoli Netview的综合网络管理平台,IBM Tivoli与Cisco一直保持着良好的 ...

  5. 【Struts 2】Struts2环境搭建

    一.关键步骤 1.创建Java Web项目 2.引入Struts2的依赖包,将依赖包拷贝到WEB-INF/lib下 * commons-logging-1.0.4.jar * freemarker-2 ...

  6. Linux设备模型 学习总结

    看LDD3中设备模型一章,觉得思维有些混乱.这里从整体的角度来理理思路.本文从四个方面来总结一些内容: 1.底层数据结构:kobject,kset.2.linux设备模型层次关系:bus_type,d ...

  7. 前端开发之-------------合理利用CSS的权重----------------

    什么是CSS的权重?对于前端工程师来说,这是一个很基础的问题,如果前端工程师没有深刻理解这个概念,则很难写出高质量的CSS代码. 那么到底什么是CSS的权重呢?我们又怎么来进行判定CSS的权重呢? 讨 ...

  8. [视频]MAC OS 技巧之如何更新及重装MAC系统

    mac os是当今最好用的桌面操作系统,但再好的系统也有新版本发布的一天,或者被极客的你尝试各种设置而配置混乱了,这时我们就要进行系统更新或者重装了. 系统更新 Mac OS有新版本推出时,会自动在A ...

  9. ASP.Net MVC 5 高级编程 第7章 成员资格、授权和安全性

    第7章 成员资格.授权和安全性 7.1 安全性 ASP.NET MVC 提供了许多内置的保护机制(默认利用 HTML 辅助方法和Razor 语法进行 HTML编码以及请求验证等功能特性,以及通过基架构 ...

  10. (六)、nodejs中的express框架获取http参数

    express获取参数方法: 一.通过req.params app.get('/user/:id', function(req, res){ res.send('user ' + req.params ...