HDU-3681-Prison Break (BFS + pressure DP + two points), hdu-3681-prisonbfs

Source: Internet
Author: User
Tags integer numbers

HDU-3681-Prison Break (BFS + pressure DP + two points), hdu-3681-prisonbfs
Problem DescriptionRompire 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.
 
InputInput 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.
OutputFor 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
 
Source2010 Asia Hangzhou Regional Contest
Idea: Use BFS to pre-process the distance between F, G, and Y, and then use the equivalent pressure DP to verify whether the result is feasible.
# Include <stdio. h> # define max (A, B) (A> B? A: B) # define INF 999999999 struct S {int x, y, step;} que [1000000], t; char mp [15] [20]; int n, m, dis [15] [15], d [20] [20], type [20], pos [20], cnt, OK, dp [1 <16] [20], nxt [4] [2] = {}, {}, {0,-1 }, {-1, 0 }}; bool vis [15] [15]; void bfs (int sx, int sy) {int I, j, top = 0, bottom = 1; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) vis [I] [j] = 0, dis [I] [j] = INF; que [0]. x = sx; que [0]. y = sy; que [0]. step = 0; dis [sx] [sy] = 0; vis [sx] [sy] = 1; while (top <bottom) {T = que [top]; t. step ++; for (I = 0; I <4; I ++) {t. x + = nxt [I] [0]; t. y + = nxt [I] [1]; if (t. x> = 0 & t. x <n & t. y> = 0 & t. y <m & mp [t. x] [t. y]! = 'D '&&! Vis [t. x] [t. y]) {vis [t. x] [t. y] = 1; dis [t. x] [t. y] = t. step; que [bottom ++] = t;} t. x-= nxt [I] [0]; t. y-= nxt [I] [1];} top ++;} bool check (int x) {int I, j, k; for (I = 0; I <(1 <cnt); I ++) for (j = 0; j <cnt; j ++) dp [I] [j] =-1; for (I = 0; I <cnt; I ++) // The remaining energy after starting from G and Y {dp [1 | (1 <I)] [I] = x-d [0] [I]; if (type [I] = 1 & dp [1 | (1 <I)] [I]> = 0) dp [1 | (1 <I)] [I] = x;} for (I = 1; I <(1 <cnt ); I ++) {if (I & 1) = 0) continue; // the start point is not in the set for (j = 0; j <cnt; j ++) {If (dp [I] [j] <0) continue; // This status cannot be expanded if (I & OK) = OK) return 1; if (I & (1 <j) // j in the set {for (k = 1; k <cnt; k ++) {if (I & (1 <k) = 0) // k is not in the set {if (dp [I] [j]> = d [j] [k]) {dp [I | (1 <k)] [k] = max (dp [I | (1 <k)] [k], dp [I] [j]-d [j] [k]); if (type [k] = 1) dp [I | (1 <k)] [k] = x; // if it is an energy pool }}}} return 0;} int main () {int I, j; while (~ Scanf ("% d", & n, & m) & n) {for (I = 0; I <n; I ++) scanf ("% s", mp [I]); cnt = 1; OK = 0; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {if (mp [I] [j] = 'F') {OK | = 1; type [0] = 0; pos [0] = I * 20 + j;} else if (mp [I] [j] = 'G ') {type [cnt] = 1; pos [cnt] = I * 20 + j; cnt ++;} else if (mp [I] [j] = 'y ') {OK | = (1 <cnt); type [cnt] = 2; pos [cnt] = I * 20 + j; cnt ++ ;}} for (I = 0; I <cnt; I ++) {bfs (pos [I]/20, pos [I] % 20); for (j = 0; j <cnt; j ++) d [I] [j] = dis [pos [j]/20] [Pos [j] % 20];} int l, r, mid, ans; l = 0; ans = r = n * m; while (l <= r) {mid = (l + r)> 1; if (check (mid) {ans = mid; r = mid-1;} else l = mid + 1 ;} printf ("% d \ n", ans <n * m? Ans:-1 );}}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.