hdu3681 状压 bfs 二分
2014-08-28 17:22
176 查看
Prison Break
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3156 Accepted Submission(s): 802
Problem Description
Rompire 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.
Input
Input 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.
Output
For 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 5 GDDSS SSSFS SYGYS SGSYS SSYSS 0 0
Sample Output
4
题意:给出一个迷宫,F为起始点,可以上下左右走,S为通路,D不能走,G为能源池(能把能量回满),Y为开关。需要把所有开关都关掉。问能量一开始最少是多少,如果不能关掉所有开关则输出-1
思路:由于最能量最少是多少所以很容易想到是用二分。要收集完所有的Y,又让人想到tsp。一开始以为说G不小于15且Y也不小于15,就觉得2^30存不下,想了老半天都没想到G怎么解决,看清楚才发现是总和(-_-#)。这就迎刃而解了,只要把表示Y的位置为1,其他表示G的位无所谓取和不取。
①先是对出发点,做一次bfs,如果某个Y点到不了,则直接输出-1
②对每个Y点和G点做bfs,找到他们之间的最短路
③二分枚举能源的初始容量,因为图的大小也就15*15,所以枚举[0,225]这个范围就够了,这里我设了上限为300
④然后是状压判断是否能收集完全部,且以某个Y点结尾的方案是大于等于0的(注意,这里能等于0,相当于开完最后一个开关然后就没能量)
需要注意的几个地方:
①一个是上面提高的,关掉最后一个开关时能量为0是可以的
②走到某个点为负就不需要记录状态,若为正需要判断该点是否为G,为G能量填满,否则就为剩余能量
③如果初始化点跟点之间最短距离为-1,那就要小心G不能走的情况,我是直接减去当前点到另一个点的距离,如果不能走,变成+1,明显不对。
最后给组数据,这组数据找到我上面的BUG
3 3
SSF
SSD
YDG
4
#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; struct sw{ int x,y,cou; sw(){}; sw(int xx,int yy):x(xx),y(yy){}; }Y[20],G[20],st,now,p; int dp[1<<15][15]; int dis[20][20][20][20]; int go[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; char map[20][20]; int n,m,yid,gid,last; void bfs(sw S){ int sx=S.x,sy=S.y; S.cou=0; dis[S.x][S.y][S.x][S.y]=0; queue<sw> Q; Q.push(S); while(!Q.empty()){ now = Q.front(); Q.pop(); p.cou=now.cou+1; for(int i=0;i<4;i++){ p.x=now.x+go[i][0]; p.y=now.y+go[i][1]; if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&map[p.x][p.y]!='D'&&dis[sx][sy][p.x][p.y]==-1){ dis[sx][sy][p.x][p.y]=p.cou; Q.push(p); } } } } bool judge(){ bfs(st); for(int i=0;i<yid;i++) if(dis[st.x][st.y][Y[i].x][Y[i].y]==-1) return true; for(int i=0;i<yid;i++) bfs(Y[i]); for(int i=0;i<gid;i++) bfs(G[i]); return false; } int getdis(int x,int y){ if(x<yid){ if(y<yid) return dis[Y[x].x][Y[x].y][Y[y].x][Y[y].y]; else return dis[Y[x].x][Y[x].y][G[y-yid].x][G[y-yid].y]; } else { if(y<yid) return dis[G[x-yid].x][G[x-yid].y][Y[y].x][Y[y].y]; else return dis[G[x-yid].x][G[x-yid].y][G[y-yid].x][G[y-yid].y]; } } bool ok(int full){ int S=(1<<(yid+gid))-1; int temp,tp=yid+gid; int res=-1; int ans=(1<<yid)-1; memset(dp,-1,sizeof(dp)); for(int i=0;i<yid;i++){ temp=full-dis[st.x][st.y][Y[i].x][Y[i].y]; if(temp<0) continue; dp[1<<i][i]=temp; if(yid==1) res=temp; } for(int i=0;i<gid;i++){ if(dis[st.x][st.y][G[i].x][G[i].y]<0) continue; temp=full-dis[st.x][st.y][G[i].x][G[i].y]; if(temp<0) continue; dp[1<<(i+yid)][i+yid]=full; } for(int i=1;i<S;i++) if(ans!=(ans&i)) for(int j=0;j<tp;j++) if(((1<<j)&i)&&dp[i][j]>0) for(int k=0;k<tp;k++) if(((1<<k)&i)==0){ int tt=getdis(j,k); if(tt<0) continue; int temp=dp[i][j]-tt; if(temp<0) continue; if(k>=yid) dp[i|(1<<k)][k]=full; else dp[i|(1<<k)][k]=max(temp,dp[i|(1<<k)][k]); if(res<0&&ans==(ans&((1<<k)|i))) res=dp[i|(1<<k)][k]; } if(res<0) return false; return true; } int slove(){ int mid,l=0,r=300; while(l<=r){ mid=(l+r)>>1; if(ok(mid)) r=mid-1; else l=mid+1; } return l; } int main(void){ while(scanf("%d%d",&n,&m),n||m){ memset(dis,-1,sizeof(dis)); last=yid=gid=0; for(int i=0;i<n;i++){ scanf("%s",map[i]); for(int j=0;map[i][j];j++){ if(map[i][j]=='G') G[gid++]=sw(i,j); else if(map[i][j]=='Y') Y[yid++]=sw(i,j); else if(map[i][j]=='F') st=sw(i,j); } } if(judge()){ printf("-1\n"); continue; } printf("%d\n",slove()); } return 0; }
相关文章推荐
- 【HDU3681】Prison Break-状态压缩DP+BFS+二分答案
- hdu3681(二分+状态压缩dp+bfs)
- HDU3681 Prison Break(壮压dp+二分+bfs)
- HDU 3681-Prison Break(状压DP+二分+BFS)
- HDU 4856 (bfs 状压DP)
- HDU 4771 BFS&状压 水
- hdu5025救唐僧 优先队列实现的BFS再加点状压 网赛题~
- HDU 3681 Prison Break (状压DP+二分)
- HDU 1429 状压bfs 胜利大逃亡
- 【POJ】2253 Frogger 二分+bfs
- BFS+状态压缩DP+二分枚举+TSP
- BFS最短路+状态dp(hdu3681)好
- hiho一下 第三十八周 二分·二分答案【二分+bfs】
- CSU 1855: Shut the Box 状压BFS(2011 Mid-Central USA Regional Contest)
- POJ 3020 Antenna Placement (二分匹配, 带花树, 状压dp)
- BFS+状态压缩DP+二分枚举+TSP
- hdu 4771 Stealing Harry Potter's Precious 状压bfs
- AOJ 2447 A Two Floors Dungeon (状压bfs)
- (BFS,状压DP)小明的迷宫
- HDU3681 Prison Break【状压】