hdu 3681 Prison Break(状态压缩+bfs)
Rompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him were put into jail, including our clever Micheal#. Now it’s time to escape, but Micheal# needs an optimal plan and he contacts you, one of his human friends, for help.
The jail area is a rectangle contains n×m little grids, each grid might be one of the following:
) Empty area, represented by a capital letter ‘S’.
) The starting position of Micheal#, represented by a capital letter ‘F’.
) Energy pool, represented by a capital letter ‘G’. When entering an energy pool, Micheal# can use it to charge his battery ONLY ONCE. After the charging, Micheal#’s battery will become FULL and the energy pool will become an empty area. Of course, passing an energy pool without using it is allowed.
) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal# cannot step into a grid with a laser sensor.
) Power switch, represented by a capital letter ‘Y’. Once Micheal# steps into a grid with a Power switch, he will certainly turn it off. In order to escape from the jail, Micheal# need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost unit of energy and only moving operation costs energy. Of course, Micheal# cannot move when his battery contains no energy. The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal# needs to make his battery as small as possible, and still large enough to hold all energy he need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal# is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.
Input contains multiple test cases, ended by . For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description of the jail.You can assume that <=n,m<=, and the sum of energy pools and power switches is less than .
For each test case, output one integer in a line, representing the minimum size of the battery Micheal# needs. If Micheal# can’t escape, output -.
GDDSS
SSSFS
SYGYS
SGSYS
SSYSS
状态压缩dp+bfs
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 17
#define inf 1e12
int n,m;
char mp[N][N];
int states;
int final_state;//要达到的目标状态
int start;
int dis[N][N][N][N];
int dp[<<N][N]; struct Node{
int x,y;
}node[N*N];
void bfs(int st){//每一点和其他点的最短距离
int x=node[st].x;
int y=node[st].y;
queue<Node>q;
q.push(node[st]);
dis[x][y][x][y]=;
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
//t2=t1;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
if(mp[t2.x][t2.y]=='D') continue;
if(dis[x][y][t2.x][t2.y]!=-) continue;
dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+;
q.push(t2);
}
}
}
bool DP(int limit){
memset(dp,-,sizeof(dp));
dp[(<<start)][start]=limit;
int res=-;
for(int i=;i<(<<states);i++){
for(int j=;j<states;j++){
if((i&(<<j))==)continue;
if(dp[i][j]==-) continue;
if((i&(final_state))==final_state){
res=max(res,dp[i][j]);
} for(int k=;k<states;k++){
if((i&(<<k))!=) continue;
if(dis[node[j].x][node[j].y][node[k].x][node[k].y]==-) continue;
if(j==k) continue;
if(dp[i][j]<dis[node[j].x][node[j].y][node[k].x][node[k].y]) continue;
dp[i|(<<k)][k]=max(dp[i|(<<k)][k],dp[i][j]-dis[node[j].x][node[j].y][node[k].x][node[k].y]);
if(mp[node[k].x][node[k].y]=='G') dp[i|(<<k)][k]=limit;
} }
}
return res>=; }
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
}
states=;
final_state=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='F'){
node[states].x=i;
node[states].y=j;
start=states;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='Y'){
node[states].x=i;
node[states].y=j;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='G'){
node[states].x=i;
node[states].y=j;
states++;
}
}
} memset(dis,-,sizeof(dis));
for(int i=;i<states;i++){
bfs(i);
}//两两之间的最短距离已求出,保存于dis int low=;
int high=;
int ans=-;
while(low<=high){
int mid=(low+high)>>;
if(DP(mid)){
ans=mid;
high=mid-;
}
else{
low=mid+;
}
}
printf("%d\n",ans); }
return ;
}
TLE代码,想不通
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 17
#define inf 1e12
int n,m;
char mp[N][N];
int states;
int final_state;//要达到的目标状态
int start;
int dis[N][N][N][N];
int dp[<<N][N];
int vis[N]; struct Node{
int x,y;
}node[N*N];
void bfs(int st){//每一点和其他点的最短距离
int x=node[st].x;
int y=node[st].y;
queue<Node>q;
q.push(node[st]);
dis[x][y][x][y]=;
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
//t2=t1;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
if(mp[t2.x][t2.y]=='D') continue;
if(dis[x][y][t2.x][t2.y]!=-) continue;
dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+;
q.push(t2);
}
}
}
/*bool DP(int limit){
memset(dp,-1,sizeof(dp));
dp[(1<<start)][start]=limit;
int res=-1;
for(int i=0;i<(1<<states);i++){
for(int j=0;j<states;j++){
if((i&(1<<j))==0)continue;
if(dp[i][j]==-1) continue;
if((i&(final_state))==final_state){
res=max(res,dp[i][j]);
} for(int k=0;k<states;k++){
if((i&(1<<k))!=0) continue;
if(dis[node[j].x][node[j].y][node[k].x][node[k].y]==-1) continue;
if(j==k) continue;
if(dp[i][j]<dis[node[j].x][node[j].y][node[k].x][node[k].y]) continue;
dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]-dis[node[j].x][node[j].y][node[k].x][node[k].y]);
if(mp[node[k].x][node[k].y]=='G') dp[i|(1<<k)][k]=limit;
} }
}
return res>=0; }
*/ bool dfs(int st,int sta,int limit,int mid){ //if(limit<0) return false; if( (sta & final_state) == final_state){
return true;
} for(int i=;i<states;i++){
if(dis[node[st].x][node[st].y][node[i].x][node[i].y]==-) continue;
if(vis[i] || limit<dis[node[st].x][node[st].y][node[i].x][node[i].y]) continue; if(mp[node[i].x][node[i].y]=='G'){
vis[i]=;
if(dfs(i,sta|(<<i),mid,mid)){
return true;
}
vis[i]=;
}
else{
vis[i]=;
if(dfs(i,sta|(<<i),limit-dis[node[st].x][node[st].y][node[i].x][node[i].y],mid)){
return true;
}
vis[i]=;
} }
return false; }
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
}
states=;
final_state=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='F'){
node[states].x=i;
node[states].y=j;
start=states;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='Y'){
node[states].x=i;
node[states].y=j;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='G'){
node[states].x=i;
node[states].y=j;
states++;
}
}
} memset(dis,-,sizeof(dis));
for(int i=;i<states;i++){
bfs(i);
}//两两之间的最短距离已求出,保存于dis int low=;
int high=;
int ans=-;
while(low<=high){
int mid=(low+high)>>;
memset(vis,,sizeof(vis));
vis[start]=;
if(dfs(start,<<start,mid,mid)){
ans=mid;
high=mid-;
}
else{
low=mid+;
}
}
printf("%d\n",ans); }
return ;
}
hdu 3681 Prison Break(状态压缩+bfs)的更多相关文章
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
- HDU 3681 Prison Break(BFS+二分+状态压缩DP)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- hdu 3681 Prison Break (TSP问题)
Prison Break Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 3681 Prison Break 越狱(状压DP,变形)
题意: 给一个n*m的矩阵,每个格子中有一个大写字母,一个机器人从‘F’出发,拾取所有的开关‘Y’时便能够越狱,但是每走一格需要花费1点能量,部分格子为充电站‘G’,每个电站只能充1次电.而且部分格子 ...
- HDU 3681 Prison Break (二分 + bfs + TSP)
题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...
- HDU 3681 Prison Break(状压DP + BFS)题解
题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...
- hdu 3681 Prison Break
http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个n*m的矩阵,'F'是起点.机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所 ...
- HDU 4634 Swipe Bo 状态压缩+BFS最短路
将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- iOS opencv
1.在iPhone上使用 OpenCV http://blog.csdn.net/kmyhy/article/details/7560472 2. OpenCV iOS Hello¶ http://d ...
- js记录用户行为浏览记录和停留时间(转)
演示地址:http://weber.pub/demo/160902/test.html 测试源码下载:http://pan.baidu.com/s/1nvPKbSP 密码:r147 解决问题所使用的知 ...
- 【DataStructure】Description and usage of queue
[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...
- uva 310 L--system(隐式图搜索+字符串处理)
L-system A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite ...
- JS滚轮事件(mousewheel/DOMMouseScroll)了解
已经没有了小学生时代过目不忘的记忆力了,很多自己折腾的东西.接触的东西,短短1年之后就全然不记得了.比方说,完全记不得获取元素与页面距离的方法(getBoundingClientRect),或者是不记 ...
- System.Speech.Synthesis 添加暂停、继续功能
为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...
- 使用repo的本地开发流程
repo下的本地开发流程 单分支开发: 1 本地新建工作目录并初始化repo库: repo init; 2 下载代码(只取服务器当前分支): repo sync -c; 3 创建本地 ...
- C++服务器设计(六):设备连接的生命周期管理
生命周期介绍 每一个服务器系统的新连接从建立开始时,均会经历多个阶段.比如连接的建立,登录的验证,退出前的资源释放等.同时在具体的消息处理中,还会遇到不可识别的消息事件,或者消息处理时出现数据错误等. ...
- c中关于#与##的简易使用
#运算符用于在预编译时,将宏参数转换为字符串 eg. #include <stdio.h>#define CONVERT(f)(#f) void helloworld(){ printf( ...
- Mysql操作个人收集
1.MySQL修改root密码 mysql> UPDATE user SET Password=PASSWORD('xxxx') where USER='root'; mysql> FLU ...