BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681
Prison Break
were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 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:
1) Empty area, represented by a capital letter ‘S’.
2) The starting position of Micheal#1, represented by a capital letter ‘F’.
3) Energy pool, represented by a capital letter ‘G’. When entering an energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’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.
4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor.
5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off.
In order to escape from the jail, Micheal#1 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 1 unit of energy and only moving operation
costs energy. Of course, Micheal#1 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#1 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#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.
of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.
5 5
GDDSS
SSSFS
SYGYS
SGSYS
SSYSS
0 0
4
题意:给出一个n行m列的矩阵,其中F代表出发的地方(只有一个),S代表空地,D代表不能经过的地方,Y代表电源开关,G代表能量充电器;
一个人要从F出发,每次只能移动周围的四个相邻的格子,没走一步消耗1单位的能量;当走到G的时候能量可以充到最大值W;问要把所有的Y都关掉的情况下;W最小是多少;否则输出-1;
分析:首先Y和G的总数小于15,可以把Y与G从图中抽取出来,然后利用bfs求得Y与G两两之间的最短路径存在dis数组中;然后二分枚举W,判断条件是状态压缩DP的值是否满足一定条件,Y状态一定要走完,但是G不一定要走完;
程序:
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M (1<<15)+2
#define eps 1e-10
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
int dp[M][16],dis[16][16],mark[16],dist[300][300],px[17],path[M][18],vis[20][20],use[20],est;
char mp[17][17];
int n,m;
int disx[5]={0,1,0,-1};
int disy[5]={1,0,-1,0};
struct node
{
int x,y,val;
}p[16];
struct Node
{
int x,y,t;
friend bool operator<(Node a,Node b)
{
return a.t>b.t;
}
};
void bfs(int x,int y)
{
priority_queue<Node>q;
memset(vis,0,sizeof(vis));
memset(dist,INF,sizeof(dist));
Node now;
now.x=x;
now.y=y;
now.t=0;
q.push(now);
vis[x][y]=1;
while(!q.empty())
{
Node cur=q.top();
q.pop();
for(int i=0;i<4;i++)
{
now.x=cur.x+disx[i];
now.y=cur.y+disy[i];
if(mp[now.x][now.y]=='D')continue;
if(now.x<0||now.y<0||now.x>=n||now.y>=m)continue;
now.t=cur.t+1;
if(dist[now.x][now.y]>now.t)
dist[now.x][now.y]=now.t;
if(vis[now.x][now.y]==0)
{
vis[now.x][now.y]=1;
q.push(now);
}
}
}
}
int DP(int cnt,int mid)//DP状压
{
int i,j,k,ff;
memset(dp,INF,sizeof(dp));
ff=0;
for(i=0;i<cnt;i++)
if(p[i].val==1)
{
ff|=(1<<i);//ff存起点F的位置
dp[1<<i][i]=0;//初始化
}
for(i=0;i<px[cnt];i++)
{
if((i&ff)!=ff)continue;//当没有起始位置的状态时跳过
for(j=0;j<cnt;j++)
{
int tep=i&(1<<j);
if(tep==0)continue;
int cur=i^(1<<j);
for(k=0;k<cnt;k++)
{
int tmp=i&(1<<k);
if(tmp==0||k==j)continue;
if(dp[i][j]>dp[cur][k]+dis[k][j])
{
if(dp[cur][k]+dis[k][j]<=mid)//在能量承受的范围内可以到达状态dp[i][j]更新;
{
dp[i][j]=dp[cur][k]+dis[k][j];
if(p[j].val==2)//如果j刚好是G则能量充满,此时把dp置为0;
dp[i][j]=0;
}
}
}
if((est&i)==est)//判断Y是否经过完
{
if(dp[i][j]<INF)
return 1;
}
/**************第二种写法********************/
/*for(k=0;k<cnt;k++)
{
int tmp=i&(1<<k);
if(tmp==0||k==j)continue;
if(dp[cur][k]>=dis[k][j])//比较需要消耗的能量是不是大于剩余的能量;
{
dp[i][j]=max(dp[i][j],dp[cur][k]-dis[j][k]);
if(p[j].val==2)//如果j刚好是G则能量充满,此时把dp能量充满;
dp[i][j]=mid;
}
}
if((est&i)==est)//判断Y是否经过完
{
if(dp[i][j]>=0)//如果不是-1则完成
return 1;
}*/
/*******************************************/
}
}
return 0;
}
int b[M];
int main()
{
int i,j;
px[0]=1;
for(i=1;i<=15;i++)
px[i]=px[i-1]*2;
while(scanf("%d%d",&n,&m),m||n)
{
for(i=0;i<n;i++)
scanf("%s",mp[i]);
int cnt=0;
est=0;
memset(p,0,sizeof(p));
for(i=0;i<n;i++)//从图中抽取出F,Y,G三个状态;
{
for(j=0;j<m;j++)
{
if(mp[i][j]=='F')
{
est=est|(1<<cnt);
p[cnt].x=i;
p[cnt].y=j;
p[cnt].val=1;
cnt++;
}
else if(mp[i][j]=='G')
{
p[cnt].x=i;
p[cnt].y=j;
p[cnt].val=2;
cnt++;
}
else if(mp[i][j]=='Y')
{
est=est|(1<<cnt);
p[cnt].x=i;
p[cnt].y=j;
p[cnt].val=3;
cnt++;
}
}
}
memset(dis,INF,sizeof(dis));
for(i=0;i<cnt;i++)
{
bfs(p[i].x,p[i].y);
for(j=0;j<cnt;j++)
{
if(i==j)dis[i][j]=INF;
else
dis[i][j]=dist[p[j].x][p[j].y];
}
}//bfs求最短路
int left=0;
int right=m*n;
int mid,ans=-1;
while(left<=right)//二分枚举W
{
mid=(left+right)/2;
if(DP(cnt,mid))
{
ans=mid;
right=mid-1;
}
else
left=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
BFS+状态压缩DP+二分枚举+TSP的更多相关文章
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分
题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...
- 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1
Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money.&quo ...
- bfs+状态压缩dp
题目连接 题解 : 对两两管道进行bfs,然后用dp[i][j] 来表示在i状态下通过了前j个管道 参考博客 #include<bits/stdc++.h> using namespace ...
- 三进制状态压缩DP(旅行商问题TSP)HDU3001
http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others) ...
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
随机推荐
- 什么是事务(transaction)?它有什么好处
为了完成对数据的操作,企业应用经常要求并发访问在多个构件之间共享的数据.这些应用在下列条件下应该维护数据的完整性(由应用的商务规则来定义): 分布式访问一个单独的数据资源,以及从一个单独的应用构件访问 ...
- e558. 在Applet中多图片交互显示
This is the simplest applet to animate an array of images. In practice, you should use double-buffer ...
- OpenCV_基于局部自适应阈值的图像二值化
在图像处理应用中二值化操作是一个很常用的处理方式,例如零器件图片的处理.文本图片和验证码图片中字符的提取.车牌识别中的字符分割,以及视频图像中的运动目标检测中的前景分割,等等. 较为常用的图像二值化方 ...
- Python3的tcp socket接收不定长数据包接收到的数据不全。
Python Socket API参考出处:http://blog.csdn.net/xiangpingli/article/details/47706707 使用socket.recv(pack_l ...
- php纯原生实现数组二分法
代码如下 $arr = [1,3,5,7,9];//$arr = range(1,10000);var_dump(find($arr, 2)); function find(array $arr, $ ...
- CSS清除浮动常用方法小结
1.使用空标签清除浮动.我用了很久的一种方法,空标签可以是div标签,也可以是P标签.我习惯用<P>,够简短,也有很多人用<hr>,只是需要另外为其清除边框,但理论上可以是任何 ...
- 基于JS实现发送短信验证码后的倒计时功能(无视页面刷新,页面关闭不进行倒计时功能)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js正则表达式的应用
JavaScript表单验证email,判断一个输入量是否为邮箱email,通过正则表达式实现. //检查email邮箱 function isEmail(str){ var reg = /^([a- ...
- JavaScript正则表达式1
在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 正则表达式可以: •数据有效性验证.可以 ...
- Maven War包 POM配置文件
如何为你的Web程序(war包设定配置文件) 约定 http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering ...