博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BFS+状态压缩DP+二分枚举+TSP
阅读量:5129 次
发布时间:2019-06-13

本文共 5571 字,大约阅读时间需要 18 分钟。

Prison Break

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3182    Accepted Submission(s): 812
Problem Description
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#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.
 
Input
Input contains multiple test cases, ended by 0 0. 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 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.
 
Output
For each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.
 
Sample Input
 
5 5 GDDSS SSSFS SYGYS SGSYS SSYSS 0 0
 
Sample Output
 
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 0x3f3f3f3fusing 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
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
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]
=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

转载于:https://www.cnblogs.com/mypsq/p/4348145.html

你可能感兴趣的文章
Tesseract 4 自行构建支持双引擎的tessdata 文件
查看>>
Find and kill the dead lock process
查看>>
CListCtrl 的应用
查看>>
mongodb
查看>>
合并指定表格指定行的相同文本的相邻单元格
查看>>
双屏幕,鼠标移动设置
查看>>
【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法
查看>>
《Entity Framework 6 Recipes》中文翻译——第十章EntityFramework存储过程处理(四)...
查看>>
收缩自编码器(CAE)
查看>>
AVL平衡树的插入例程
查看>>
Android Studio怎么删除项目
查看>>
shell变量自增 || Python脚本接收参数
查看>>
关于python安装lxml插件的问题
查看>>
Exp7 网络欺诈防范
查看>>
婴姿坊微商城前端开发总结
查看>>
完成评论功能
查看>>
关于小数计算引发的定点数思考
查看>>
三态门实现“一读多写”总线结构
查看>>
C#读取word模版并对指定域写入数据保存为新word
查看>>
java通过dom读写xml文件
查看>>