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 ...
随机推荐
- JSTL解析——007——fmt标签库02
各位亲们,近期事情比较多,没更新,come on! 1.<fmt:bundle>/<fmt:message>/<fmt:param>资源国际化标签 java中使用R ...
- ora-28056 (Writing audit records to Windows Event Log failed)
系统:windows xp oracle 版本 SQL> select * from v$version; BANNER ------------------------------------ ...
- HHKB Professional 2
今天买了一副号称程序员专用的静电容键盘 HHBK Pro2无刻版,简单上手了一下,确实名不虚传,打起字来酣畅淋漓,毫不拖泥带水,感觉自己的技术提高了不少呢!!!! 由于是无刻版,需要一些时间来适应,尤 ...
- thinkphp实现excel数据的导入导出
下载地址:phpexcel.rar 实现步骤: 一:在http://phpexcel.codeplex.com/下载最新PHPExcel放到Vendor下,注意位置:ThinkPHP\Extend\V ...
- 使用Open Flash Chart(OFC)制作图表(Struts2处理)
Java开源项目中制作图表比较出色的就是JFreeChart了,相信大家都听说过,它不仅可以做出非常漂亮的柱状图,饼状图,折线图基本图形之外,还能制作甘特图,仪表盘等图表.在Web应用中可以为项目增色 ...
- Highcharts 非常实用的Javascript统计图
Highcharts 官网: http://www.highcharts.com Highcharts 官网示例:http://www.highcharts.com/demo/ Highcharts ...
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...
- js中json的转换
//aa='{"id":0,"appId":"app***********Id","appSecret":"a ...
- poi HSSFRichTextString 对cell中的每一段文字设置字体
HSSFRichTextString ts= new HSSFRichTextString(" 经审核,我司已同意其出库申请.请你部按规定将托管银行承兑汇票办理出库." + &qu ...
- Linux命令查询手册--sort
一.sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. 二.sort的参数选项 1. sort -u 在输出 ...