Key Task

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 990    Accepted Submission(s):
378

Problem 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 ≤ R, C ≤ 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.
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std; int n,m;
char a[][];
bool dp[][][(<<)+];
int map1[][]={{,},{,},{,-},{-,}};
struct node
{
int x,y;
int state;
int time;
};
queue<node>Q; int get(char cc)
{
if(cc=='B' || cc=='b')return ;
else if(cc=='Y' || cc=='y')return ;
else if(cc=='R' || cc=='r')return ;
else return ;
}
int bfs(int x,int y)
{
int i,x1,y1,state;
struct node t,cur; t.x=x;
t.y=y;
t.time=;
t.state=;
dp[x][y][]=true;
Q.push(t);
while(!Q.empty())
{
t=Q.front();
Q.pop();
for(i=;i<;i++)
{
x1=t.x+map1[i][];
y1=t.y+map1[i][];
if(x1>=&&x1<n && y1>=&&y1<m && a[x1][y1]!='#')
{
if(a[x1][y1]=='X')
{
printf("Escape possible in %d steps.\n",t.time+);
return ;
}
if(a[x1][y1]>='A'&&a[x1][y1]<='Z')
{
state=(<<get(a[x1][y1]));
if( (t.state&state)==state && dp[x1][y1][t.state]==false)
{
dp[x1][y1][t.state]=true;
cur=t;
cur.time++;
cur.x=x1;
cur.y=y1;
Q.push(cur);
}
}
else if(a[x1][y1]>='a'&&a[x1][y1]<='z')
{
state=(<<get(a[x1][y1]));
cur=t;
cur.state=(cur.state|state);
if(dp[x1][y1][cur.state]==false)
{
dp[x1][y1][cur.state]=true;
cur.x=x1;
cur.y=y1;
cur.time++;
Q.push(cur);
}
}
else if(dp[x1][y1][t.state]==false)
{
dp[x1][y1][t.state]=true;
cur.x=x1;
cur.y=y1;
cur.state=t.state;
cur.time=t.time+;
Q.push(cur);
}
}
}
}
return -;
}
int main()
{
int i,j,x,y,k;
bool cur;
while(scanf("%d%d",&n,&m)>)
{
if(n==&&m==)break;
memset(dp,false,sizeof(dp));
while(!Q.empty())
{
Q.pop();
}
for(i=;i<n;i++)
scanf("%s",a[i]);
cur=false;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(a[i][j]=='*')
{
x=i;
y=j;
}
if(a[i][j]=='X')
cur=true;
}
}
if(cur==false) printf("The poor student is trapped!\n");
else
{
k=bfs(x,y);
if(k==-)printf("The poor student is trapped!\n");
}
}
return ;
}

hdu 1885的更多相关文章

  1. HDU 1885 Key Task(三维BFS)

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

  2. hdu 1885 Key Task

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...

  3. hdu 1885 Key Task(bfs)

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

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

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

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

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

  6. hdu 1885 Key Task (三维bfs)

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

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

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

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

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

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

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

随机推荐

  1. Day 32 网络编程

    一.网络协议基础篇 一台完整的计算机由硬件.系统.软件组成,具备这三个条件,计算机就可以运行,但是只能自己和自己玩.为了实现计算机和计算机间的连接,就需要借助互联网,如全世界人类交流将英语作为标准语言 ...

  2. django 自定义中间件 middleware

    Django 中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强 ...

  3. 【AUTO Uninstaller 中文版-详细使用教程】AUTODESK系列软件MAYA/CAD/3DSMAX/INVENTOR终极完美修复卸载工具 Windows x64位 【原创搬家】

    小伙伴是不是遇到 MAYA/CAD/3DSMAX/INVENTOR/REVIT 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR/ ...

  4. Flask从入门到精通之跨站请求伪造保护

    默认情况下,Flask-WTF 能保护所有表单免受跨站请求伪造(Cross-Site Request Forgery,CSRF)的攻击.恶意网站把请求发送到被攻击者已登录的其他网站时就会引发CSRF ...

  5. 克隆linux虚拟机

    背景:有时候,我们在用虚拟机的时候会用到多个进行使用.重新安装会花费大量的时间,此时,我们可以通过vmware虚拟机自带的功能快速克隆出完全相同的系统. 前提:被克隆的虚拟机系统要处于关闭状态 步骤: ...

  6. 【wireshark】插件开发(一):概述

    1. 概述 wireshark提供了灵活的插件机制,使用户可以方便地扩展wireshark的功能.插件的功能主要包括,但不限于协议解析器. 可以使用Lua或C语言来编写Wireshark插件,下表对比 ...

  7. Sublime Text 3快捷键汇总

    转自:http://blog.sina.com.cn/s/blog_73c5cfbe0101ldj8.html Sublime Text 3非常实用,但是想要用好,一些快捷键不可或缺,所以转了这个快捷 ...

  8. out.print()与out.write()的区别

    out对象的类型是JspWriter.JspWriter继承了java.io.Writer类. 1)print方法是子类JspWriter,write是Writer类中定义的方法: 2)重载的prin ...

  9. Ubuntu16.04 / OpenCV / Python 源码安装

    为什么需要源码安装? 1. 对 Python 版的 OpenCV,Ubuntu 有两种安装方式: 源码安装:官网(https://opencv.org/releases.html)下载源代码,在机器上 ...

  10. Spring Security构建Rest服务-0702-个性化用户认证流程2

    登录成功后的处理AuthenticationSuccessHandler: 认证成功后,默认情况下spring security会继续访问之前访问的url,如果想自定义处理逻辑,用默认的就不行了.此时 ...