HDU3681 Prison break (like pressure DP)

Source: Internet
Author: User

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


Title: robot from F, go to G can charge, go to y turn off the switch, D can not walk in, require all the switch off, and the least amount of electricity, and to find the minimum power.


Idea: Because we have to get to the Y F, maybe to the G, we have to set the state to the following code of all, the state is Len, and then according to the current state can update which state to be DP, the problem is not clear where the G, the topic said this point can be charged, can not charge, Maybe next time to recharge, but it is best not to think about it, to the G-Charge it







#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; #define FRE (i,a,b) for (i = A; I <b; i++) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SSF (a) SC ANF ("%s", a) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A,B,C) SC  ANF ("%d%d", &a, &b, &c) #define PF printf#define Bug pf ("hi\n") using namespace std; #define    INF 0x3f3f3f3f#define N 20char a[n][n];int dis[n][n][n][n];int k;int n,m;int first,all;int dp[1<<16][16];  Dp[s][i] s state the last to reach I point also has oil int step[4][2]={1,0,-1,0,0,1,0,-1};int vis[n][n];struct stud{Stud () {}; Stud (int _x,int _y): X (_x), Y (_y), time (0) {} stud (int _x,int _y,int _time): X (_x), Y (_y), TimE (_time) {} int x, y; int time;}    f[n*n];inline bool Judge (int x,int y) {if (x>=0&&x<n&&y>=0&&y<m) return true; return false;}    void BFs (int t) {queue<stud>q;    int i,j;    f[t].time=0;    Q.push (F[t]);    Stud cur;    CUR=F[T];    dis[cur.x][cur.y][cur.x][cur.y]=0;    MEM (vis,0);    Vis[cur.x][cur.y]=1;    while (!q.empty ()) {Cur=q.front ();    Q.pop ();    Fre (i,0,4) {int xx=cur.x+step[i][0];    int yy=cur.y+step[i][1];   if (judge (xx,yy) &&a[xx][yy]!= ' D ' &&!vis[xx][yy]) {dis[f[t].x][f[t].y][xx][yy]=cur.time+1;    The distance between the starting point and the Point Vis[xx][yy]=1;q.push (Stud (xx,yy,cur.time+1));} }}}bool Check (int now) {int I,j;mem (dp,-1);DP [1<<first][first]=now;int cur,len=1<<k;int To,ans=-1;fre (   Cur,0,len) Fre (i,0,k) {if ((cur& (All)) ==all) Ans=max (Ans,dp[cur][i]);  Go through the point of the Walk if (ans!=-1) return true; if (! (    cur& (1<<i))) continue;   Cur should be traversed by I if (dp[cur][i]==-1) continue; This state did not go over to frE (to,0,k)//Enumerate the next point {if (cur& (1<<to)) continue;   if (dis[f[i].x][f[i].y][f[to].x][f[to].y]==-1) continue;  Both cannot reach if (i==to) continue;   int TIME=DP[CUR][I]-DIS[F[I].X][F[I].Y][F[TO].X][F[TO].Y];  From I go to the remaining oil if (time<0) continue;  int next=cur+ (1<<to);  Dp[next][to]=max (Dp[next][to],time);  if (a[f[to].x][f[to].y]== ' G ') Dp[next][to]=now; If you arrive at the point can be refueling//(in fact, I feel there is a bug, maybe others first to this point does not refuel, the next time to refuel)}} return false;    int solve ()//two cent answer {int le,ri,mid;int ans=300;le=0;ri=300;         int I=0;while (Le<=ri) {mid= (Le+ri) >>1; if (check (mid))//Determine if the mid can satisfy the condition {ans=mid; ri=mid-1;}    elsele=mid+1;} if (ans==300) Return-1;return ans;}    int main () {int i,j; Freopen ("In.txt", "R", stdin), while (~SFF (n,m), n+m) {fre (i,0,n) SSF (A[i]), All=0;k=0;fre (i,0,n) fre (j,0,m) {if (a[i][           j]== ' F ') {all|=1<<k;  We have to go. The final state of all points is all first=k;  F[k++]=stud (I,J); } elseif (a[i][j]== ' Y ') {all|=1<<k;  F[k++]=stud (I,J); } elseif (a[i][j]== ' G ') F[k++]=stud (I,J);    } int T,tt;fre (i,0,20) fre (j,0,20) fre (t,0,20) fre (tt,0,20) dis[i][j][t][tt]=-1;fre (i,0,k) BFS (i);       Find out the distance int ans=solve () of any two points; PF ("%d\n", ans);} return 0;}



HDU3681 Prison break (like 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.