博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3681 Prison Break(状态压缩+bfs)
阅读量:6233 次
发布时间:2019-06-22

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

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 5GDDSSSSSFSSYGYSSGSYSSSYSS0 0

 

 

 

Sample Output
4

 

 

 

Source
 

 状态压缩dp+bfs

1 #pragma comment(linker, "/STACK:1024000000,1024000000")  2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 using namespace std; 16 int dirx[]={ 0,0,-1,1}; 17 int diry[]={-1,1,0,0}; 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[1<
q; 41 q.push(node[st]); 42 dis[x][y][x][y]=0; 43 Node t1,t2; 44 while(!q.empty()){ 45 t1=q.front(); 46 q.pop(); 47 for(int i=0;i<4;i++){ 48 //t2=t1; 49 t2.x=t1.x+dirx[i]; 50 t2.y=t1.y+diry[i]; 51 if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue; 52 if(mp[t2.x][t2.y]=='D') continue; 53 if(dis[x][y][t2.x][t2.y]!=-1) continue; 54 dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+1; 55 q.push(t2); 56 } 57 } 58 } 59 bool DP(int limit){ 60 memset(dp,-1,sizeof(dp)); 61 dp[(1<
=0; 83 84 } 85 int main() 86 { 87 while(scanf("%d%d",&n,&m)==2){ 88 if(n==0 && m==0){ 89 break; 90 } 91 states=0; 92 final_state=0; 93 for(int i=0;i
>1;127 if(DP(mid)){128 ans=mid;129 high=mid-1;130 }131 else{132 low=mid+1;133 }134 }135 printf("%d\n",ans);136 137 }138 return 0;139 }
View Code

 

TLE代码,想不通

1 #pragma comment(linker, "/STACK:1024000000,1024000000")  2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 using namespace std; 16 int dirx[]={ 0,0,-1,1}; 17 int diry[]={-1,1,0,0}; 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[1<
q; 42 q.push(node[st]); 43 dis[x][y][x][y]=0; 44 Node t1,t2; 45 while(!q.empty()){ 46 t1=q.front(); 47 q.pop(); 48 for(int i=0;i<4;i++){ 49 //t2=t1; 50 t2.x=t1.x+dirx[i]; 51 t2.y=t1.y+diry[i]; 52 if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue; 53 if(mp[t2.x][t2.y]=='D') continue; 54 if(dis[x][y][t2.x][t2.y]!=-1) continue; 55 dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+1; 56 q.push(t2); 57 } 58 } 59 } 60 /*bool DP(int limit){ 61 memset(dp,-1,sizeof(dp)); 62 dp[(1<
=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 93 if( (sta & final_state) == final_state){ 94 return true; 95 } 96 97 for(int i=0;i
>1;162 memset(vis,0,sizeof(vis));163 vis[start]=1;164 if(dfs(start,1<
View Code

 

转载地址:http://cdqna.baihongyu.com/

你可能感兴趣的文章
Hyper-V下面激活win2008 R2虚拟机系统
查看>>
python判断两个对象是否相等
查看>>
润乾集算报表呈现输出之页内统计
查看>>
【MVVM】- Avalon 属性监控、解除监控、子孙元素监控、统一属性监控
查看>>
Python开发中的cookie 学习
查看>>
Jmeter中的几个重要测试指标释义
查看>>
Thumbnailator java图片压缩,加水印,批量生成缩略图
查看>>
【VMCloud云平台】SCSM(九)SCSM创建服务产品
查看>>
JSTL中的fn函数
查看>>
16_Shell语言———for循环元素列表的生成法则
查看>>
计算机学习经典书籍
查看>>
使用beanuti将bean属性转成map
查看>>
apache以mod_proxy实现负载均衡集群
查看>>
dsfsdfsdfsdfsdfsdfsdf
查看>>
linux Platform设备驱动
查看>>
侦探推理小故事
查看>>
IPTV监测和测试设备
查看>>
rsync添加多模块,客户端推送出错!
查看>>
如何使用 Java8 实现观察者模式?(上)
查看>>
网络基础之--IP数据报、分片策略、路由表
查看>>