题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681

思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题。首先bfs预处理出‘Y',’F','G'之间的最短距离,由于G点可以充电,到达G点就把当前能量更新为电池容量然后继续走。因为每个G点只能充一次电,这就好像TSP中的每个点只能走一次一样,然后就是二分答案了,用状压DP判定当前电池容量的情况下是否能符合条件。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; struct Node{
int x,y;
}node[*]; int dist[][][][];
int dp[<<][];
int n,m,state,final_state,start;
char map[][];
int dir[][]={{-,},{,},{,-},{,}}; void bfs(Node &node)
{
queue<pair<int,int> >que;
que.push(make_pair(node.x,node.y));
dist[node.x][node.y][node.x][node.y]=;
while(!que.empty()){
int x=que.front().first,y=que.front().second;
que.pop();
for(int i=;i<;i++){
int xx=x+dir[i][],yy=y+dir[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&map[xx][yy]!='D'){
if(dist[node.x][node.y][xx][yy]==-){
dist[node.x][node.y][xx][yy]=dist[node.x][node.y][x][y]+;
que.push(make_pair(xx,yy));
}
}
}
}
} bool Judge(int Power)
{
memset(dp,-,sizeof(dp));
dp[(<<start)][start]=Power;
int res=-;
for(int i=;i<(<<state);i++){
for(int j=;j<state;j++){
if((i&(<<j))==)continue;
if(dp[i][j]<)continue;
if(((i&final_state)&final_state)==final_state)res=max(res,dp[i][j]);
for(int k=;k<state;k++){
if((i&(<<k))||(j==k))continue;
if(dist[node[j].x][node[j].y][node[k].x][node[k].y]<)continue;
if(dp[i][j]-dist[node[j].x][node[j].y][node[k].x][node[k].y]<)continue;
dp[i|(<<k)][k]=max(dp[i|(<<k)][k],dp[i][j]-dist[node[j].x][node[j].y][node[k].x][node[k].y]);
if(map[node[k].x][node[k].y]=='G')dp[i|(<<k)][k]=Power;
}
}
}
return res>=;
} int main()
{
int low,high,mid,ans;
while(~scanf("%d%d",&n,&m)){
if(n==&&m==)break;
state=;
final_state=;
for(int i=;i<n;i++){
scanf("%s",map[i]);
for(int j=;j<m;j++){
if(map[i][j]=='F'){
start=state;
node[state].x=i,node[state].y=j;
final_state|=(<<state);
state++;
}else if(map[i][j]=='G'){
node[state].x=i,node[state++].y=j;
}else if(map[i][j]=='Y'){
node[state].x=i,node[state].y=j;
final_state|=(<<state);
state++;
}
}
}
memset(dist,-,sizeof(dist));
for(int i=;i<state;i++){
bfs(node[i]);
}
low=,high=,ans=-;
while(low<=high){
mid=(low+high)>>;
if(Judge(mid)){
ans=mid;
high=mid-;
}else
low=mid+;
}
printf("%d\n",ans);
}
return ;
}

hdu 3681(bfs+二分+状压dp判断)的更多相关文章

  1. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  2. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  4. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  5. HDU 4272 LianLianKan(状压DP)题解

    题意:一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去 思路:一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去.因为还要判断栈顶有没 ...

  6. HDU 4272 LianLianKan (状压DP+DFS)题解

    思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...

  7. HDU 3362 Fix (状压DP)

    题意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的, 要求总长度最短. 析:由于这个 n 最大才是18,比较小 ...

  8. HDU 3001 Travelling (状压DP,3进制)

    题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...

  9. HDU 2923 Relocation(状压dp+01背包)

    题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...

随机推荐

  1. webDriver API——第10部分Chrome WebDriver

    class selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, chrome_o ...

  2. windows下检測软件的网络连接

    首先打开任务管理器选中你要查看的应用.右键转到进程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFwZW5nMDExMg==/font/5a6L5L2 ...

  3. 关于天津中软国际ETC培训中心的见与闻

    半个月的时间转眼即逝,在中软国际天津ETC的实训的两个星期就这样过去了,在中软国际天津站的这段时间也收获了非常多.明确了非常多,了解了很多关于社会就业的知识以及关于软件培训的一些事情.尽管我的了解可能 ...

  4. No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).

    No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing inst ...

  5. C#调用 oracle存储过程

    C#调用oracle 存储过程与调用Sql server存储过程类似,比较简单:直接给出示例: /// <summary> /// 判断物料类型是不是总部管控 /// </summa ...

  6. crc32 根据字符串获取校验值

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text; n ...

  7. FreeBSD Top States

    转自:http://blog.csdn.net/fcoolx/article/details/4412196 select Process is blocked in the select(2) sy ...

  8. Postman---html中get和post的区别和使用

    get和post的区别和使用 Html中post和get区别,是不是用get的方法用post都能办到? Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DEL ...

  9. res与res-auto的区别

    Solution: Upgrade to latest SDK & ADT version (fixed was released since r17) and usehttp://schem ...

  10. js 获取距离顶部的相对高度

    getTop (e) { var offset=e.offsetTop; if(e.offsetParent!=null) offset+=this.getTop(e.offsetParent); r ...