传送门:

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

Key Task

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

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.
 
Source
 
Recommend
linle   |   We have carefully selected several similar problems for you:  1882 1887 1889 1883 1888 
 
分析:
这道题和HDU1429是胜利大逃亡(续)是同一类型的题目,属于模板题,
但是这题需要注意的地方是*代表起点,X代表终点,可能有多个终点,还有就是X终点最好改为其他字符,不然会与门弄混淆,可以改为^
还有就是只有4把钥匙,所以三维数组开(1<<4)+10大小就可以了
太大了不行,会超内存所以我们改一下模板就可以了
具体的做法请参考这篇博客:
 
code:
#include<bits/stdc++.h>
using namespace std;
#define max_v 105
char G[max_v][max_v];//图
int dis[max_v][max_v][(<<)+];//步数
int dir[][]= {{-,},{,-},{,},{,}}; //方向数组
int n,m;//行,列,限定时间
int sx,sy;//起点
struct node
{
int x,y;
int key;
node(int a,int b,int c)
{
x=a;
y=b;
key=c;
}
}; inline int get_key(int key,int num)//返回新的钥匙集合
{
//参数:元素的钥匙集合 活动钥匙的编号
return key|(<<num);
} inline bool has_key(int key,int num)//返回是否存在门的钥匙
{
//参数:钥匙集合 门的编号
return (key&(<<num))>;
}
int bfs()
{
//初始化
queue<node> q;
int step=-;
memset(dis,-,sizeof(dis)); q.push(node(sx,sy,));
dis[sx][sy][]=; while(!q.empty())
{
int x=q.front().x;
int y=q.front().y;
int key=q.front().key;
q.pop(); if(G[x][y]=='^')
{
step =dis[x][y][key];
return key;
}
for(int i=; i<; i++)
{
int xx=x+dir[i][];
int yy=y+dir[i][];
int kk=key; if(xx<||xx>=n||yy<||yy>=m||G[xx][yy]=='#')//越界和墙
continue;
if(G[xx][yy]>='a'&&G[xx][yy]<='j')//遇到了钥匙
{
kk=get_key(kk,G[xx][yy]-'a');//返回新的钥匙集合
}
if(G[xx][yy]>='A'&&G[xx][yy]<='J')//遇到了门
{
if(!has_key(kk,G[xx][yy]-'A'))//没有对应的钥匙
{
continue;
}
}
if(dis[xx][yy][kk]==-)
{
dis[xx][yy][kk]=dis[x][y][key]+;//步数加1 if(G[xx][yy]=='^')//放这里是因为路上有门的特殊性
{
step = dis[xx][yy][kk];
return step;
} q.push(node(xx,yy,kk));
}
}
}
return step;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)
break; for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
scanf("\n%c",&G[i][j]);
if(G[i][j]=='*')
{
sx=i;//起点
sy=j;
}
if(G[i][j]=='X')
{
G[i][j]='^';
}
if(G[i][j]=='Y')
{
G[i][j]='A';
}
if(G[i][j]=='R')
{
G[i][j]='C';
}
if(G[i][j]=='G')
{
G[i][j]='D';
}
if(G[i][j]=='y')
{
G[i][j]='a';
}
if(G[i][j]=='r')
{
G[i][j]='c';
}
if(G[i][j]=='g')
{
G[i][j]='d';
}
}
}
int ans=bfs();
if(ans==-)
{
printf("The poor student is trapped!\n");
}
else
{
printf("Escape possible in %d steps.\n",ans);
}
}
return ;
}
 
 
 

HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)的更多相关文章

  1. hdu 1885 Key Task

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

  2. HDU 1885 Key Task(三维BFS)

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

  3. hdu 1885 Key Task (三维bfs)

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

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

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

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

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

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

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

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

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

  8. hdu 1885 Key Task(bfs)

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

  9. 【HDOJ】1885 Key Task

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

随机推荐

  1. Rabbit的事务

    加入事务的方法: txSelect()  txCommit()  txRollback() 生产者: package com.kf.queueDemo.transactions; import jav ...

  2. Spring cloud ReadTimeout 问题解决

    今天使用Spring cloud @FeignClient 调用远程服务的时候,出现readTimeout问题,通过找资料解决方式如下 在Spring.properties 配置文件中添加如下属性解决 ...

  3. thrift简介

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...

  4. 一、IOC和DI的概念

    IOC---Inversion of Control (控制反转) 在java中,IOC意味着将你设计好的对象交给容器控制,而不是传统的在你对象内部直接控制. 谁控制谁,控制什么  -->IOC ...

  5. hdu 4055 Number String(递推DP)

    给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于. 问1~n的排列中有多少个满足该字符串. http ...

  6. Csharp:WebClient and WebRequest use http download file

    //Csharp:WebClient and WebRequest use http download file //20140318 塗聚文收錄 string filePath = "20 ...

  7. Open images from USB camera on linux using V4L2 with OpenCV

    I have always been using OpenCV's VideoCapture API to capture images from webcam or USB cameras. Ope ...

  8. 02_NIO简单实例

    [一个用NIO实现的客户端向服务端单向通信的例子] [服务端程序] package com.nio.test; import java.io.IOException; import java.net. ...

  9. Java不带.classpath的svn项目下载,转成到eclipse中

    .classpath是Eclipse的工程文件,别人没有将工程的信息传到SVN库中,就检查不出.classpath文件,识别不了项目结构. 这种做法也是比较提倡的方法.SVN上只要有项目的源码信息就可 ...

  10. 程序单一实例实现 z

    不少应用程序有单一实例的需求,也就是同时只能开启一个实例(一般也就是一个进程). 实现的方式可能有判断进程名字,使用特殊文件等等,但是最靠谱的方式还是使用系统提供的 Mutex 工具. Mutex是互 ...