HDU 3681 Prison Break (bfs+ binary + pressure DP)

Source: Internet
Author: User

Prison breakTime limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3778 Accepted Submission (s): 992


Problem Descriptionrompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire is captured by human beings. His thinking circuit is changed by human and thus became a tyrant. All those who is against him were put into jail and including our clever micheal#1. Now it's time to escape, but micheal#1 needs a optimal plan and he contacts you, one of its human friends, for help.
The jail area was a rectangle contains nxm 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 a energy pool, micheal#1 can use it to charge he battery only ONCE. After the charging, micheal#1 ' s battery would become full and the energy pool would 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 to a grid with a laser sensor.
5) Power switch, represented by a capital letter ' Y '. Once micheal#1 steps to a grid with a Power switch, he'll 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-T Hen he can just fly away. Moving to a adjacent grid (directly up, down, left or right) would cost 1 unit of energy and only moving operation costs E Nergy. Of course, micheal#1 cannot move when he battery contains no energy.

The larger the battery is and 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 the battery as small as possible, and still large enough to the all energy he need. Assuming that the size of the battery equals to maximum units of energy so can be saved in the battery, and micheal#1 are Fully charged at the beginning, please tell him the minimum a size of the battery needed for he Prison break.

Inputinput contains multiple test cases, ended by 0 0. For each test case, the first line contains the 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 this 1<=n,m<=15, and the sum of energy pools and power switches are 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

/* Test instructions: D can't go, S can go, y is to reach the place (so all to pass), G is the place to refuel (a G can only be added once by) F is the starting point, ask the minimum amount of time to be able to go through all the y thinking: Y is a must to the place, G is selective to, with the shape pressure 1: , F,g minimum distance 2: Two-point answer, and then use the pressure DP to determine the feasibility of */#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) # Define EPS 1e-8typedef __int64 ll;using namespace std; #define INF 0x3f3f3f3f#define N 16int dp[1<<n][n];int all,k;in  T dis[n][n][n][n];int n,m;struct stud{Stud () {};  Stud (int xx,int yy): X (xx), Y (yy), step (0) {};  int x, y; int step;} F[n];queue<stud>q;int Step[4][2]={1,0,-1,0,0,1,0,-1};char c[n][n];int vis[n][n];int start;bool judge (int x,int Y) {if (x>=0&&x<n&&y>=0&&y<m) return True;return false;}   void BFs (int sx,int sy) {int i,j;   while (!q.empty ()) Q.pop (); memset (vis,0,sizeof (VIS));  Stud cur (sx,sy);   Cur.step=0;   Q.push (cur);   Stud next;   Vis[sx][sy]=1;      while (!q.empty ()) {Cur=q.front ();      Q.pop ();    for (i=0;i<4;i++) {int xx=cur.x+step[i][0];    int yy=cur.y+step[i][1];    if (!judge (XX,YY)) continue;    if (c[xx][yy]== ' D ') continue;    if (Vis[xx][yy]) continue;    Next.x=xx;    Next.y=yy;    next.step=cur.step+1;    Dis[sx][sy][xx][yy]=next.step;    Vis[xx][yy]=1;   Q.push (next); }}}inline int Getdis (int i,int j) {return dis[f[i].x][f[i].y][f[j].x][f[j].y];}    BOOL DP (int mid) {int i,j;    int len=1<<k,ans=-1;    Memset (Dp,-1,sizeof (DP)); dp[1<<start][start]=mid;    starting point int cur; for (cur=0;cur<len;cur++) for (i=0;i<k;i++) {if (!) (     cur& (1<<i))) continue;     if ((Cur&all) ==all) Ans=max (Ans,dp[cur][i]);     if (dp[cur][i]<=0) continue;    for (j=0;j<k;j++) {if (cur& (1<<j)) continue; int next=cur|    (1&LT;&LT;J);    int Temp=getdis (I,J); if (Temp>dp[cur][i]) continue;   Dp[next][j]=max (dp[next][j],dp[cur][i]-temp);  if (c[f[j].x][f[j].y]== ' G ') dp[next][j]=mid; }} return ans>=0;} void Solve () {int i,j;int le=0,ri=250,mid;//two-point answer int ans=-1;while (Le<=ri) {mid= (Le+ri) >>1;if (DP (mid)) {ans= Mid;ri=mid-1;}    elsele=mid+1;} printf ("%d\n", ans);} int main () {int i,j;while (scanf ("%d%d", &n,&m), n+m) {for (i=0;i<n;i++) scanf ("%s", C[i]); K=0;all=0;for (i=0;       i<n;i++) for (j=0;j<m;j++) if (c[i][j]== ' F ') {all|=1<<k; Start=k;         Beginning F[k++]=stud (I,J); }else if (c[i][j]== ' G ') {F[k++]=stud (i,j);}  ElseIf (c[i][j]== ' Y ') {all|=1<<k; The state to be reached is Allf[k++]=stud (I,J);}     memset (dis,inf,sizeof (DIS)), for (i=0;i<k;i++) BFS (F[I].X,F[I].Y); solve ();} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3681 Prison Break (bfs+ binary + pressure DP)

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.