BFS+状态压缩 hdu-1885-Key Task
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1885
题目意思:
给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应颜色的钥匙才能打开,问到达终点的最短步数。
解题思路:
BFS+状态压缩。
将每种颜色对应一个二进制数位,1表示已经得到该颜色的钥匙,0表示没有得到。
一把钥匙可以同种颜色的多扇门。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; /*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/ map<char,int>myp1,myp2; bool vis[20][110][110];
char save[110][110];
int n,m,dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; struct Point
{
int x,y,step,ss;
}s; bool iscan(int x,int y)
{
if(x<1||x>n||y<1||y>m||save[x][y]=='#')
return false;
return true;
}
//一把钥匙可以开颜色相同的多种锁
void bfs()
{
memset(vis,false,sizeof(vis));
s.step=0,s.ss=0;
queue<Point>myq; myq.push(s);
vis[0][s.x][s.y]=true;
while(!myq.empty())
{
Point tmp=myq.front();
myq.pop(); int xx,yy; for(int i=0;i<4;i++)
{
xx=tmp.x+dir[i][0],yy=tmp.y+dir[i][1];
if(!iscan(xx,yy))
continue;
if(save[xx][yy]=='X')
{
printf("Escape possible in %d steps.\n",tmp.step+1);
return ;
} int tt=tmp.ss;
if(myp1.find(save[xx][yy])!=myp1.end()) //得到一把钥匙
{
tt=tt|(1<<myp1[save[xx][yy]]);
if(vis[tt][xx][yy]) //该状态之前已被访问
continue;
}
else if(myp2.find(save[xx][yy])!=myp2.end())
{
if(!(tt&(1<<myp2[save[xx][yy]]))) //没有钥匙
continue;
}
if(vis[tt][xx][yy])
continue;
vis[tt][xx][yy]=true;
Point t;
t.x=xx,t.y=yy,t.ss=tt,t.step=tmp.step+1;
myq.push(t);
}
}
printf("The poor student is trapped!\n");
return ;
} int main()
{
myp1['b']=0,myp1['y']=1,myp1['r']=2,myp1['g']=3; //每种不同的颜色对应不同的数位
myp2['B']=0,myp2['Y']=1,myp2['R']=2,myp2['G']=3; while(scanf("%d%d",&n,&m)&&m+n)
{
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%s",save[i]+1);
for(int j=1;j<=m;j++)
{
if(save[i][j]=='*')
s.x=i,s.y=j;
}
}
bfs();
} return 0;
}
BFS+状态压缩 hdu-1885-Key Task的更多相关文章
- HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others) ...
- hdu 1885 Key Task
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- hdu 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
- hdu 1885 Key Task(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...
随机推荐
- MVC中Area的使用
1.Area是什么? MVC 2 中引进了区域的概念,它允许将模型,视图和控制器分成单独的功能节点,换句话说,可以在大型复杂的网站中建立几个区域(模块),每一个区域都有Model,View,Contr ...
- $_REQUEST变量数组header()函数
$_SERVER 包含http信息头,路径和服务器端的一些信息,没发送一次HTTP请求,就会创建一个$_SERVER数组Array ( [HTTP_HOST] => localhost [HTT ...
- 快速生成json实体类
读取一个json文件,并与实体相对应: static void Main(string[] args) { string json = ""; FileStream fs = ne ...
- xampp install
the way of restart: sudo /opt/lampp/lampp restart config root: /opt/lampp/etc/httpd.conf /opt/lampp/ ...
- JVM 找出最耗 cpu的线程 并打印线程栈
监控JVM中最占cpu的线程 top -Hp pid JVM中最占cpu的线程ID -o THREAD,tid,time | awk 'BEGIN {count=0; } { if($2>0.3 ...
- NULL, nil, Nil详解
原文地址:http://blog.csdn.net/wzzvictory/article/details/18413519 感谢原作者 作者:wangzz 原文地址:http://blog.cs ...
- Linux Screen命令使用
参考URL: http://jingyan.baidu.com/article/295430f128d8ea0c7e005089.html ~~~~~~~~~~~~~~~~~~~~~~~~ 其它的不提 ...
- 使用HttpURLConnection向服务器发送post和get请求
一.使用HttpURLConnection向服务器发送get请求 1.向服务器发送get请求 @Test publicvoid sendSms() throws Exception{ String m ...
- [wikioi]石子归并
http://wikioi.com/problem/1048/ 区间型动态规划.参考PPT:http://wenku.baidu.com/view/73c1ded5b9f3f90f76c61bc4.h ...
- 转载:java保留2位小数
转载:http://blog.csdn.net/wj_j2ee/article/details/8560132 java保留两位小数问题: 方式一: 四舍五入 double f = 11 ...