HDU 3681 Prison Break (State compression DP + BFS + binary answer)

Source: Internet
Author: User
Tags integer numbers
Prison Break

Time Limit: 5000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2178 accepted submission (s): 533

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 input5 5
Gddss
Sssfs
Sygys
Sgsys
Ssyss
0 0

 

Sample output4

 

Source2010 Asia Hangzhou Regional Contest

 

Recommendlcy & Zhengfeng N * M matrix, 'F' is the starting point. The robot starts from F, can go to g to charge, can go to Y to turn off the switch, D can not enter, need to turn off all the switches, and the minimum power, and find the minimum power. First, use BFs to find the distance between two points. It can be seen that 'g' and 'y' can only be used once, and then the status is compressed to DP for solving. Use the binary method to obtain the answer.
 /*  * HDU 3681 * Status compression DP + BFS + binary answer  */  # Include <Iostream> # Include <Stdio. h> # Include < String . H> # Include <Algorithm># Include <Queue> Using   Namespace  STD;  Const   Int Maxn = 17  ;  Struct  Node {  Int  X, Y ;};  Char  STR [maxn] [maxn];  Int Dis [maxn] [maxn] [maxn] [maxn];//  Dis [SX] [sy] [Ex] [ey] indicates the shortest distance from (sx, Sy) to (ex, ey), and-1 indicates that it cannot be reached.  Int  N, m; node [maxn];  Int  CNT;  Int  Start;  Int  Finalstate; queue <Node> Q;  Int Move [] [ 2 ] = {{ 1 , 0 },{-1 , 0 },{ 0 , 1 },{ 0 ,- 1  }};  Void BFS ( Int SX, Int Sy) //  BFS calculates the distance from (sx, Sy) to other points  {  If (STR [SX] [sy] = ' D  ' ) Return ; //  This point does not need to be processed      For ( Int I = 0 ; I <n; I ++ )  For ( Int J = 0 ; J <m; j ++ ) Dis [SX] [sy] [I] [J] =- 1 ;  While (! Q. Empty () Q. Pop (); node TMP; TMP. x = Sx, TMP. Y = Sy; DIS [SX] [sy] [SX] [sy] = 0  ; Q. Push (TMP); node now;  While (! Q. Empty () {TMP = Q. Front (); q. Pop ();  For ( Int I = 0 ; I < 4 ; I ++ ) {Now. x = TMP. x + move [I] [ 0  ]; Now. Y = TMP. Y + move [I] [ 1  ];  If (Now. x < 0 | Now. x> = n | now. Y < 0 | Now. Y> = m) Continue  ;  If (STR [now. x] [now. Y] = '  D ' ) Continue  ;  If (DIS [SX] [sy] [now. x] [now. Y]! =- 1 ) Continue  ; DIS [SX] [sy] [now. x] [now. Y] = Dis [SX] [sy] [TMP. x] [TMP. Y] + 1  ; Q. Push (now );}}}  Int DP [ 1 < Maxn] [maxn];  Bool Check (Int V) //  Determine whether the v volume meets the requirements  {Memset (DP, - 1 , Sizeof  (DP); DP [  1 <Start] [start] = V;  For ( Int I = 0 ; I <( 1 <CNT); I ++ ) For ( Int J = 0 ; J <CNT; j ++ ){  If (I &( 1 <J) = 0 ) Continue  ;  If (DP [I] [J] =- 1 ) Continue  ;  If (I & finalstate) = finalstate) Return   True ; //  Target arrival status              For ( Int K = 0 ; K <CNT; k ++ ){  If (I = j | (I &( 1 <K ))! = 0 ) Continue  ; Int TMP = Dis [node [J]. x] [node [J]. Y] [node [K]. x] [node [K]. Y];  If (TMP =- 1 | DP [I] [J] <TMP) Continue  ; DP [I | ( 1 <K)] [k] = max (DP [I | ( 1 <K)] [K], DP [I] [J]- TMP );  If (STR [node [K]. x] [node [K]. Y] = '  G ' ) DP [I | ( 1 <K)] [k] = V ;}}  Return   False  ;}  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      While (Scanf ( "  % D " , & N, & M) = 2  ){  If (N = 0 & M = 0 ) Break  ;  For ( Int I = 0 ; I <n; I ++) scanf ( "  % S  "  , STR [I]); For ( Int I = 0 ; I <n; I ++ )  For ( Int J = 0 ; J <m; j ++ ) BFS (I, j); CNT = 0  ; Finalstate = 0  ;  For ( Int I =0 ; I <n; I ++ )  For ( Int J = 0 ; J <m; j ++ ){  If (STR [I] [J] = '  F  ' ) //  Start Point  {Start = CNT; finalstate | = ( 1 < CNT); node [CNT]. x = I; node [CNT]. Y = J; CNT ++ ;}  Else   If (STR [I] [J] = '  G  '  ) {Node [CNT]. x = I; node [CNT]. Y =J; CNT ++ ;}  Else   If (STR [I] [J] = '  Y  '  ) {Finalstate | = ( 1 < CNT); node [CNT]. x = I; node [CNT]. Y = J; CNT ++;}}  Int L = 0 , R = N * M;  Int Ans =- 1  ;  While (L <= r) //  Result:  {  Int Mid = (L + r )/ 2  ;  If (Check (MID) {ans = Mid; R = Mid- 1  ;}  Else L = Mid + 1  ;} Printf (  "  % D \ n  "  , ANS );}  Return   0  ;} 

 

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.