Problem Description
  1. 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.
  2. The jail area is a rectangle contains n×m little grids, each grid might be one of the following:
  3. ) Empty area, represented by a capital letter S’.
  4. ) The starting position of Micheal#, represented by a capital letter ‘F’.
  5. ) 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.
  6. ) Laser sensor, represented by a capital letter D’. Since it is extremely sensitive, Micheal# cannot step into a grid with a laser sensor.
  7. ) Power switch, represented by a capital letter Y’. Once Micheal# steps into a grid with a Power switch, he will certainly turn it off.
  8.  
  9. 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.
  10.  
  11. 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
  1. 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 .
 
Output
  1. 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 -.
Sample Input
  1. GDDSS
  2. SSSFS
  3. SYGYS
  4. SGSYS
  5. SSYSS
 
Sample Output
  1.  
 
Source
 

状态压缩dp+bfs

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. int dirx[]={,,-,};
  17. int diry[]={-,,,};
  18. #define PI acos(-1.0)
  19. #define max(a,b) (a) > (b) ? (a) : (b)
  20. #define min(a,b) (a) < (b) ? (a) : (b)
  21. #define ll long long
  22. #define eps 1e-10
  23. #define MOD 1000000007
  24. #define N 17
  25. #define inf 1e12
  26. int n,m;
  27. char mp[N][N];
  28. int states;
  29. int final_state;//要达到的目标状态
  30. int start;
  31. int dis[N][N][N][N];
  32. int dp[<<N][N];
  33.  
  34. struct Node{
  35. int x,y;
  36. }node[N*N];
  37. void bfs(int st){//每一点和其他点的最短距离
  38. int x=node[st].x;
  39. int y=node[st].y;
  40. queue<Node>q;
  41. q.push(node[st]);
  42. dis[x][y][x][y]=;
  43. Node t1,t2;
  44. while(!q.empty()){
  45. t1=q.front();
  46. q.pop();
  47. for(int i=;i<;i++){
  48. //t2=t1;
  49. t2.x=t1.x+dirx[i];
  50. t2.y=t1.y+diry[i];
  51. if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
  52. if(mp[t2.x][t2.y]=='D') continue;
  53. if(dis[x][y][t2.x][t2.y]!=-) continue;
  54. dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+;
  55. q.push(t2);
  56. }
  57. }
  58. }
  59. bool DP(int limit){
  60. memset(dp,-,sizeof(dp));
  61. dp[(<<start)][start]=limit;
  62. int res=-;
  63. for(int i=;i<(<<states);i++){
  64. for(int j=;j<states;j++){
  65. if((i&(<<j))==)continue;
  66. if(dp[i][j]==-) continue;
  67. if((i&(final_state))==final_state){
  68. res=max(res,dp[i][j]);
  69. }
  70.  
  71. for(int k=;k<states;k++){
  72. if((i&(<<k))!=) continue;
  73. if(dis[node[j].x][node[j].y][node[k].x][node[k].y]==-) continue;
  74. if(j==k) continue;
  75. if(dp[i][j]<dis[node[j].x][node[j].y][node[k].x][node[k].y]) continue;
  76. 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]);
  77. if(mp[node[k].x][node[k].y]=='G') dp[i|(<<k)][k]=limit;
  78. }
  79.  
  80. }
  81. }
  82. return res>=;
  83.  
  84. }
  85. int main()
  86. {
  87. while(scanf("%d%d",&n,&m)==){
  88. if(n== && m==){
  89. break;
  90. }
  91. states=;
  92. final_state=;
  93. for(int i=;i<n;i++){
  94. scanf("%s",mp[i]);
  95. for(int j=;j<m;j++){
  96. if(mp[i][j]=='F'){
  97. node[states].x=i;
  98. node[states].y=j;
  99. start=states;
  100. final_state+=(<<states);
  101. states++;
  102. }
  103. else if(mp[i][j]=='Y'){
  104. node[states].x=i;
  105. node[states].y=j;
  106. final_state+=(<<states);
  107. states++;
  108. }
  109. else if(mp[i][j]=='G'){
  110. node[states].x=i;
  111. node[states].y=j;
  112. states++;
  113. }
  114. }
  115. }
  116.  
  117. memset(dis,-,sizeof(dis));
  118. for(int i=;i<states;i++){
  119. bfs(i);
  120. }//两两之间的最短距离已求出,保存于dis
  121.  
  122. int low=;
  123. int high=;
  124. int ans=-;
  125. while(low<=high){
  126. int mid=(low+high)>>;
  127. if(DP(mid)){
  128. ans=mid;
  129. high=mid-;
  130. }
  131. else{
  132. low=mid+;
  133. }
  134. }
  135. printf("%d\n",ans);
  136.  
  137. }
  138. return ;
  139. }

TLE代码,想不通

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. int dirx[]={,,-,};
  17. int diry[]={-,,,};
  18. #define PI acos(-1.0)
  19. #define max(a,b) (a) > (b) ? (a) : (b)
  20. #define min(a,b) (a) < (b) ? (a) : (b)
  21. #define ll long long
  22. #define eps 1e-10
  23. #define MOD 1000000007
  24. #define N 17
  25. #define inf 1e12
  26. int n,m;
  27. char mp[N][N];
  28. int states;
  29. int final_state;//要达到的目标状态
  30. int start;
  31. int dis[N][N][N][N];
  32. int dp[<<N][N];
  33. int vis[N];
  34.  
  35. struct Node{
  36. int x,y;
  37. }node[N*N];
  38. void bfs(int st){//每一点和其他点的最短距离
  39. int x=node[st].x;
  40. int y=node[st].y;
  41. queue<Node>q;
  42. q.push(node[st]);
  43. dis[x][y][x][y]=;
  44. Node t1,t2;
  45. while(!q.empty()){
  46. t1=q.front();
  47. q.pop();
  48. for(int i=;i<;i++){
  49. //t2=t1;
  50. t2.x=t1.x+dirx[i];
  51. t2.y=t1.y+diry[i];
  52. if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
  53. if(mp[t2.x][t2.y]=='D') continue;
  54. if(dis[x][y][t2.x][t2.y]!=-) continue;
  55. dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+;
  56. q.push(t2);
  57. }
  58. }
  59. }
  60. /*bool DP(int limit){
  61. memset(dp,-1,sizeof(dp));
  62. dp[(1<<start)][start]=limit;
  63. int res=-1;
  64. for(int i=0;i<(1<<states);i++){
  65. for(int j=0;j<states;j++){
  66. if((i&(1<<j))==0)continue;
  67. if(dp[i][j]==-1) continue;
  68. if((i&(final_state))==final_state){
  69. res=max(res,dp[i][j]);
  70. }
  71.  
  72. for(int k=0;k<states;k++){
  73. if((i&(1<<k))!=0) continue;
  74. if(dis[node[j].x][node[j].y][node[k].x][node[k].y]==-1) continue;
  75. if(j==k) continue;
  76. if(dp[i][j]<dis[node[j].x][node[j].y][node[k].x][node[k].y]) continue;
  77. 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]);
  78. if(mp[node[k].x][node[k].y]=='G') dp[i|(1<<k)][k]=limit;
  79. }
  80.  
  81. }
  82. }
  83. return res>=0;
  84.  
  85. }
  86. */
  87.  
  88. bool dfs(int st,int sta,int limit,int mid){
  89.  
  90. //if(limit<0) return false;
  91.  
  92. if( (sta & final_state) == final_state){
  93. return true;
  94. }
  95.  
  96. for(int i=;i<states;i++){
  97. if(dis[node[st].x][node[st].y][node[i].x][node[i].y]==-) continue;
  98. if(vis[i] || limit<dis[node[st].x][node[st].y][node[i].x][node[i].y]) continue;
  99.  
  100. if(mp[node[i].x][node[i].y]=='G'){
  101. vis[i]=;
  102. if(dfs(i,sta|(<<i),mid,mid)){
  103. return true;
  104. }
  105. vis[i]=;
  106. }
  107. else{
  108. vis[i]=;
  109. if(dfs(i,sta|(<<i),limit-dis[node[st].x][node[st].y][node[i].x][node[i].y],mid)){
  110. return true;
  111. }
  112. vis[i]=;
  113. }
  114.  
  115. }
  116. return false;
  117.  
  118. }
  119. int main()
  120. {
  121. while(scanf("%d%d",&n,&m)==){
  122. if(n== && m==){
  123. break;
  124. }
  125. states=;
  126. final_state=;
  127. for(int i=;i<n;i++){
  128. scanf("%s",mp[i]);
  129. for(int j=;j<m;j++){
  130. if(mp[i][j]=='F'){
  131. node[states].x=i;
  132. node[states].y=j;
  133. start=states;
  134. final_state+=(<<states);
  135. states++;
  136. }
  137. else if(mp[i][j]=='Y'){
  138. node[states].x=i;
  139. node[states].y=j;
  140. final_state+=(<<states);
  141. states++;
  142. }
  143. else if(mp[i][j]=='G'){
  144. node[states].x=i;
  145. node[states].y=j;
  146. states++;
  147. }
  148. }
  149. }
  150.  
  151. memset(dis,-,sizeof(dis));
  152. for(int i=;i<states;i++){
  153. bfs(i);
  154. }//两两之间的最短距离已求出,保存于dis
  155.  
  156. int low=;
  157. int high=;
  158. int ans=-;
  159. while(low<=high){
  160. int mid=(low+high)>>;
  161. memset(vis,,sizeof(vis));
  162. vis[start]=;
  163. if(dfs(start,<<start,mid,mid)){
  164. ans=mid;
  165. high=mid-;
  166. }
  167. else{
  168. low=mid+;
  169. }
  170. }
  171. printf("%d\n",ans);
  172.  
  173. }
  174. return ;
  175. }

hdu 3681 Prison Break(状态压缩+bfs)的更多相关文章

  1. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

  2. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  3. hdu 3681 Prison Break (TSP问题)

    Prison Break Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. HDU 3681 Prison Break 越狱(状压DP,变形)

    题意: 给一个n*m的矩阵,每个格子中有一个大写字母,一个机器人从‘F’出发,拾取所有的开关‘Y’时便能够越狱,但是每走一格需要花费1点能量,部分格子为充电站‘G’,每个电站只能充1次电.而且部分格子 ...

  5. HDU 3681 Prison Break (二分 + bfs + TSP)

    题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...

  6. HDU 3681 Prison Break(状压DP + BFS)题解

    题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...

  7. hdu 3681 Prison Break

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个n*m的矩阵,'F'是起点.机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所 ...

  8. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

  9. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. Linux如何生成列表

    如何生成列表: 方法一:{1..100} 方法二:`seq [起始数 [步进长度]] 结束数` 1,...,100 declare -i SUM=0    integer    -x

  2. JAX-WS 学习一:基于java的最简单的WebService服务

    JAVA 1.6 之后,自带的JAX-WS API,这使得我们可以很方便的开发一个基于Java的WebService服务. 基于JAVA的WebService 服务 1.创建服务端WebService ...

  3. python list 按长度分段

    def changes(a,b): #list 分段函数,a:数据[(1),(2)]b:长度 for i in xrange(0,len(a),b): yield  a[i:i+b] for i in ...

  4. 将string 转int

    /** * @param {string} str * @return {number} */ var myAtoi = function(str) { str = str.replace(/^(\s ...

  5. J2EE之普通类载入web资源文件的方法

    在WEB中普通类并不能像Servlet那样通过this.getServletContext().getResourceAsStream()获取web资源,须要通过类载入器载入,这里有两种方式,这两种方 ...

  6. 一、cocos2dx概念简介

    cocos2dx概念介绍 1)scene,继承自CCScene 场景,一个游戏运行期间的显示界面,一个应用里面可以有多个场景,但是每次只能有一个是激活状态,也可以理解为一次只能显示一个界面. 例如,你 ...

  7. Ubuntu 系统 文件操作命令

    文件和目录的操作 用户主目录下有一个 Desktop (对应,桌面)mkdir dir1 建立一个目录cd 不添加参数,默认回到主目录(用户目录)touch a.txt 建立一个文件mv a.txt ...

  8. sql语句中查询出的数据添加一列,并且添加默认值

    查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的 select app_id,app_secret from wx_ticket group by app_id; 查询出的数据是 ...

  9. SQL Server两种分页的存储过程介绍

          由于现在很多的企业招聘的笔试都会让来招聘的写一个分页的存储过程,有的企业甚至要求应聘者用两种方式实现分页,如果没有在实际项目中使用过分页,那么很多的应聘者都会出现一定的问题,下面介绍两种分 ...

  10. 没有暑假的Ada 要好好努力咯 C#继续