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 );}}
HDU-3681-Prison break (BFS + pressure DP + two points)