Prison Break

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3214    Accepted Submission(s): 829

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
 

思路:首先提取出各个特殊点(开关位置、充电池、起始位置),对它们进行标号。然后,求它们之间的最短距离。

之后。二分枚举可能的初始能量值,进行DP看是否能走到目标状态。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 20
const int inf=10000;
int n1,n2,n,m,f;
char g[N][N];
int x[N],y[N];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int dis[N][N],dist[N][N];
int dp[1<<15][N];
void inti() //读入数据。把特殊点标号
{
n1=0;
int i,j;
for(i=0; i<n; i++)
{
scanf("%s",g[i]);
for(j=0; j<m; j++)
{
if(g[i][j]=='F')
{
f=n1;
x[n1]=i;
y[n1++]=j;
}
else if(g[i][j]=='Y')
{
x[n1]=i;
y[n1++]=j;
}
}
}
n2=n1;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
if(g[i][j]=='G')
{
x[n2]=i;
y[n2++]=j;
}
}
void bfs(int x,int y,int dis[][20]) //求一个特殊点到其它各个点最短路
{
int i,j,u,v;
queue<int>q1,q2;
q1.push(x);
q2.push(y);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
dis[i][j]=inf;
dis[x][y]=0;
while(!q1.empty())
{
x=q1.front();
y=q2.front();
q1.pop();
q2.pop();
for(i=0;i<4;i++)
{
u=x+dir[i][0];
v=y+dir[i][1];
if(u<0||u>=n||v<0||v>=m||g[u][v]=='D')
continue;
if(dis[u][v]>dis[x][y]+1)
{
dis[u][v]=dis[x][y]+1;
q1.push(u);
q2.push(v);
}
}
}
}
bool findd(int t)
{
int i,j,k,lim=1<<n2,tmp=(1<<n1)-1;
for(i=0;i<lim;i++)
for(j=0;j<n2;j++)
dp[i][j]=-inf;
dp[1<<f][f]=t;
for(i=1<<f;i<lim;i++)
{
for(j=0;j<n2;j++)
{
if(dp[i][j]<0)
continue;
if((i&tmp)==tmp) //不一定要相等。包括目标状态即可
return true;
for(k=0;k<n2;k++)
{
int p=1<<k;
if(i&p)
continue;
dp[i|p][k]=max(dp[i|p][k],dp[i][j]-dis[j][k]);
if(dp[i|p][k]>=0&&k>=n1)
dp[i|p][k]=t;
}
}
}
return false;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),n||m)
{
inti();
for(i=0; i<n2; i++)
{
bfs(x[i],y[i],dist);
for(j=0; j<n2; j++) //记录标号后的点相互之间距离
{
dis[i][j]=dist[x[j]][y[j]];
} //i到j的距离等于i点到各个点的最短路
} int l=0,r=300,flag=0;
while(l<=r) //二分枚举各个可能的能量值
{
int mid=(l+r)/2;
if(findd(mid))
{
flag=1;
r=mid-1;
}
else
l=mid+1;
}
if(!flag)
l=-1;
printf("%d\n",l);
}
return 0;
}

hdu 3681 Prison Break (TSP问题)的更多相关文章

  1. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  2. hdu 3681 Prison Break(状态压缩+bfs)

    Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...

  3. HDU 3681 Prison Break (二分 + bfs + TSP)

    题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...

  4. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

  5. hdu 3681 Prison Break

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个n*m的矩阵,'F'是起点.机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所 ...

  6. HDU 3681 Prison Break 越狱(状压DP,变形)

    题意: 给一个n*m的矩阵,每个格子中有一个大写字母,一个机器人从‘F’出发,拾取所有的开关‘Y’时便能够越狱,但是每走一格需要花费1点能量,部分格子为充电站‘G’,每个电站只能充1次电.而且部分格子 ...

  7. HDU 3681 Prison Break(状压DP + BFS)题解

    题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...

  8. hdu3511 Prison Break 圆的扫描线

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511 题目: Prison Break Time Limit: 10000/5000 MS ( ...

  9. HDU3681 Prison Break

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. Oracle的函数返回表类型(转)

    在SQL Server中有表变量,可以在function中方便地返回,习惯SQL Server或者需要把脚本从SQL Server转到Oracle中的朋友可以都会碰到这个问题. Oracle的func ...

  2. struct 与 typedef struct

    1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typede ...

  3. 重新开始吧(ADB+AndroidManifest.xml)

    我现在默认已经搭建好了开发环境.如果没有,可以参见去Google一下,或者我上两篇文章中也有提到. 先补充一点: SDK不用FQ.也能更新 修改hosts文件 下载sdk版本: 在hosts文件中追加 ...

  4. 三年程序学习之二:(对web初认识)

    接着上一篇讲,之后第二天我就来公司上班了,主要是前端,CSS+DIV,table,网站维护之类的,这样的日子过了将近3个星期,一直没什么进展,自己也学不到什么技术,不过我觉得CSS+DIV我算是基础的 ...

  5. KMP算法的代码实现

    上周算法班的BEN老师花了1个小时讲自动机和KMP的关系,结果failed...明天又要上课了,花了半天时间看了下KMP,暂且停留在利用next求模式中的跳跃长度,自动机那个还不能理解... 具体的可 ...

  6. 两个队列+k叉哈夫曼树 HDU 5884

    // 两个队列+k叉哈夫曼树 HDU 5884 // camp题解: // 题意:nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过TT, ...

  7. <System.ServiceModel>

    實例: <system.serviceModel>    <diagnostics performanceCounters="All" />    < ...

  8. java webservice的多种实现方法汇总

    一.基于EJB容器管理webservice :     1.首先建立一个Web services EndPoint: package cn.test.service.impl; import java ...

  9. Systemd Unit文件中PrivateTmp字段详解-Jason.Zhi

    如下图,在开发调试的时候会遇到这么一个问题. file_put_contents时,$tmp_file显示的目标文件是/tmp/xxx.而这个文件实际放在linux的目录却是/tmp/systemd- ...

  10. ajax 新闻栏目

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...