HDU 3681 Prison Break
来源:互联网 发布:手机表白神器软件 编辑:程序博客网 时间:2024/06/06 03:06
Prison Break
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4726 Accepted Submission(s): 1288
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.
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
2010 Asia Hangzhou Regional Contest
Recommend
lcy&zhengfeng | We have carefully selected several similar problems for you: 3682 3685 3686 3683 3689
思路:因为题目说了Y点和G点的总和<=15,所以我们把所有的F、Y、G点保存起来然后用二进制状态压缩表示,然后求出所有的F、Y、G点中两两的距离,跑一遍bfs就好了,最后用二分枚举答案跑遍状压dp。下面给代码:
#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<iostream>#include<algorithm>#include<cmath>#include<bitset>#include <utility>using namespace std;#define maxn 16#define inf 0x3f3f3f3ftypedef long long LL;const int mod = 1e9 + 7;int dp[1 << maxn][maxn], dis[maxn][maxn], mm[maxn][maxn], num, steparr[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 }, st, re, n, m;char s[maxn][maxn];struct node{int x, y;}p[maxn];void bfs(int a){queue<node>q;q.push(p[a]);int vis[maxn][maxn];memset(vis, -1, sizeof(vis));vis[p[a].x][p[a].y] = 0;while (!q.empty()){node now = q.front();q.pop();for (int i = 0; i < 4; i++){int nextx = now.x + steparr[i][0];int nexty = now.y + steparr[i][1];if (s[nextx][nexty] == 'D' || ~vis[nextx][nexty] || nextx < 0 || nextx >= n || nexty < 0 || nexty >= m)continue;vis[nextx][nexty] = vis[now.x][now.y] + 1;q.push(node{ nextx, nexty });if (~mm[nextx][nexty]){dis[a][mm[nextx][nexty]] = dis[mm[nextx][nexty]][a] = vis[nextx][nexty];}}}}bool jud(int now){memset(dp, -1, sizeof(dp));dp[1 << st][st] = now;for (int i = st; i < (1 << num); i++){for (int j = 0; j < num; j++){if (i&(1 << j)&&~dp[i][j]){for (int k = 0; k < num; k++){if (!(i&(1 << k))&&~dis[j][k]){dp[i | (1 << k)][k] = max(dp[i | (1 << k)][k], dp[i][j] - dis[j][k]);if (dp[i | (1 << k)][k] >= 0){if (((i | (1 << k))&re) == re)return true;if (s[p[k].x][p[k].y] == 'G')dp[i | (1 << k)][k] = now;}}}}}}return false;}int main(){while (scanf("%d%d", &n, &m) && n&&m){num = 0;re = 0;memset(mm, -1, sizeof(mm));memset(dis, -1, sizeof(dis));for (int i = 0; i < n; i++){scanf("%s", s[i]);for (int j = 0; j < m; j++){if (s[i][j] == 'F'){st = num;p[num].x = i;p[num].y = j;re |= 1 << num;mm[i][j] = num++;}else if (s[i][j] == 'G'){p[num].x = i;p[num].y = j;mm[i][j] = num++;}else if (s[i][j] == 'Y'){p[num].x = i;p[num].y = j;re |= 1 << num;mm[i][j] = num++;}}}for (int i = 0; i < num; i++){bfs(i);}int l = 0, r = n*m;int ans = -1;while (l <= r){int mid = l + r >> 1;if (jud(mid)){r = mid - 1;ans = mid;}elsel = mid + 1;}printf("%d\n", ans);}}
0 0
- HDU 3681 - Prison Break
- HDU 3681 Prison Break
- HDU 3681Prison Break
- HDU 3681 Prison Break
- HDU 3681 Prison Break
- hdu 3681 Prison Break
- hdu 3681 Prison Break bfs+二分+dp
- hdu 3681 Prison Break (旅行商问题)
- HDU 3681 Prison Break (搜索+状压dp)
- hdu 3681 Prison Break(dp || dfs)
- hdu 3681 Prison Break 扫描线set
- hdu 3681 Prison Break (TSP问题)
- HDU 3681 Prison Break floyd+状压+二分
- hdu 3681 Prison Break (BFS+状压)
- HDU 3681 Prison Break (状压DP+二分)
- hdu 3681 Prison Break bfs + 二分 + TSP
- 【搜索+状压DP】 HDU 3681 Prison Break
- HDU 3681 Prison Break - 状压dp【TSP】
- view getLeft, getRight, getTop, getBottom
- Python内置函数_数学运算类
- 使用原子主机、Ansible 和 Cockpit 部署容器
- springMVC源码分析--国际化LocaleResolver(一)
- 6-this、static关键字
- HDU 3681 Prison Break
- 【PS】利用滤镜制作巧克力效果
- 简图记录-shell的基本使用
- 提升服务效率就这么简单
- 微信小程序常见 错误
- ubuntu首次安装后必装的软件(by 星空武哥)
- Linux内核移植 part4:USB概念梳理
- UICollectionView的cell间距问题
- 好像这玩意叫离散余弦变换?