题目链接

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

思路 : 这个题比赛的时候完全没有思路,想了好久脑子都疼了也没想出来。原来是一个三维的BFS,因为要拿钥匙的缘故可能有些地方要来回走,所以不能像以前标记二维一样只要走过的点就不能走了,这里用三维来标记,你要是走过这个地方并且钥匙数都是一样的,那就不用再走了,因为那代表你又走回来了,看了网上大神写的学到了一个新的表示钥匙的方法,按位与,要不就要用数组什么的太麻烦。

 //HDU 1885
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <iostream> using namespace std ; struct node
{
int x,y,step ;
int keys ;
} p[],temp,temp1 ; int n,m ;
int key[] = {'b','y','r','g'} ;
int door[] = {'B','Y','R','G'} ;
int dir[][] = {{,-},{,},{-,},{,}} ;
bool vis[][][( << ) + ] ;
char mapp[][] ; void BFS(int sx,int sy)
{
queue<node>Q ;
temp.x = sx ;
temp.y = sy ;
temp.step = temp.keys = ;
Q.push(temp) ;
vis[sx][sy][] = true ;
while(!Q.empty())
{
temp = Q.front() ;
Q.pop() ;
if(mapp[temp.x][temp.y] == 'X')
{
printf("Escape possible in %d steps.\n",temp.step) ;
return ;
}
for(int i = ; i < ; i++)
{
temp1.x = temp.x + dir[i][] ;
temp1.y = temp.y + dir[i][] ;
temp1.step = temp.step+;
temp1.keys = temp.keys ; if(mapp[temp1.x][temp1.y] == '#' || temp1.x < || temp1.x >= n || temp1.y < || temp1.y >= m)
continue ;
else if(islower(mapp[temp1.x][temp1.y]))
{
for(int k = ; k < ; k++)
{
if(mapp[temp1.x][temp1.y] == key[k])
{
if((temp1.keys & ( << k)) == )//如果这把钥匙没有,就加上。
temp1.keys += ( << k) ; }
if(!vis[temp1.x][temp1.y][temp1.keys])
{
Q.push(temp1) ;
vis[temp1.x][temp1.y][temp1.keys] = true ;
//break ;
}
}
}
else if(isupper(mapp[temp1.x][temp1.y]) && mapp[temp1.x][temp1.y] != 'X')
{
for(int k = ; k < ; k++)
{
if(mapp[temp1.x][temp1.y] == door[k])
{
if(temp1.keys & ( << k))
{
if(!vis[temp1.x][temp1.y][temp1.keys])
{
vis[temp1.x][temp1.y][temp1.keys] = true ;
Q.push(temp1) ; }
break ;
}
}
}
}
else
{
if(!vis[temp1.x][temp1.y][temp1.keys])
{
vis[temp1.x][temp1.y][temp1.keys] = true ;
Q.push(temp1) ;
}
}
}
}
printf("The poor student is trapped!\n") ;
} int main()
{
int sx,sy ;
while(~scanf("%d %d",&n,&m))
{
if(n == && m == ) break ;
for(int i = ; i < n ; i++)
scanf("%s",mapp[i]) ;
memset(vis,false,sizeof(vis)) ;
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
if(mapp[i][j] == '*')
{
sx = i ;
sy = j ;
}
}
}
BFS(sx,sy) ;
}
return ;
}

HDU 1885 Key Task(三维BFS)的更多相关文章

  1. hdu 1885 Key Task (三维bfs)

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

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

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

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

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

  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

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

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

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

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

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

  8. hdu 1885 Key Task(bfs)

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

  9. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. 免费主机kilu使用

    我也是看了这篇文章:http://www.cnblogs.com/tenny/archive/2011/03/30/1999957.html 采取申请注册. 主机申请地址:http://www.kil ...

  2. JAVA 实现通过URL下载文件到本地库

    /** * TODO 下载文件到本地 * @author nadim * @date Sep 11, 2015 11:45:31 AM * @param fileUrl 远程地址 * @param f ...

  3. 第六篇、微信小程序-form组件

    表单: 将组件内的用户输入的<switch/> <input/> <checkbox/> <slider/> <radio/> <pi ...

  4. Swift泛型和泛型函数

    泛型(generic)可以使我们在程序代码中定义一些可变的部分,在运行的时候指定.使用泛型可以最大限度地重用代码.保护类型的安全以及提高性能.在Swift集合类中,已经采用了泛型.一.一个问题的思考怎 ...

  5. Windows Forms (一)

    导读 1.什么是 Windows Forms 2.需要学Windows Forms 么? 3.如何手写一个简单的Windows Forms 程序 4.对上面程序的说明 5.Form 类与Control ...

  6. c语言与c++基础知识

    1.后缀名: C++/C程序的头文件以.h为后缀,C程序的源文件以.c为后缀,C++程序的源文件通常以.cpp为后缀(有些书中介绍有一些系统以.cc或.cxx为后缀的源文件).在Linux系统下的gc ...

  7. 真正的inotify+rsync实时同步 彻底告别同步慢

    真正的inotify+rsync实时同步 彻底告别同步慢       http://www.ttlsa.com/web/let-infotify-rsync-fast/     背景 我们公司在用in ...

  8. nutch安装配置

    http://nlp.solutions.asia/?p=180 http://www.promenade.me/archives/146 环境 ubuntu 12.04 sql建表 CREATE D ...

  9. sql2005下载和安装

    下载地址:个人百度网盘 http://pan.baidu.com/s/1kTvKIZd sql05安装包.rar 1.54G这个 安装方法: 先安装Tool 在安装Server http://bbs. ...

  10. (转载)SQL语句,纵列转横列

    SQL语句,纵列转横列 Feed: 大富翁笔记 Title: SQL语句,纵列转横列 Author: wzmbox Comments sTable.db库位 货物编号 库存数1 0101 501 01 ...