Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of curiosity and he wants to visit all of the secret tunnels beneath the city. To travel in a tunnel, he has to walk to the entrance of the tunnel and go out from the exit after a fabulous visit. He can choose where he starts and he will travel each of the tunnels once and only once. Now he wants to know, how long it will take him to visit all the tunnels (excluding the time when he is in the tunnels).

Input
The input contains mutiple testcases. Please process till EOF.
For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels.
The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”.
Then M lines follow. Each line consists of four integers x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y 1) and (x2, y2) in the map are both empty grid.

Output
For each case, output a integer indicating the minimal time Bob will use in total to walk between tunnels.
If it is impossible for Bob to visit all the tunnels, output -1.
Sample Input

5 4
....#
...#.
.....
.....
.....
2 3 1 4
1 2 3 5
2 3 3 1
5 4 2 1

Sample Output

7

因为数组开小了,错了不知道多少次,心态爆炸,最关键的是自己一直没有意识到这个问题
状态压缩DP,将集合中某个元素是否存在用二进制表示,如1001,表示元素1和元素4在集合中,而元素2,3不在。
状态的转移采用位运算的方法,确定i不在这个集合中,加入元素i,转移到新的状态,也就是变成了一个新的集合。
因为从1开始遍历,以1111为例。 这四个元素都在的集合,其二进制表示为1111,而能转移到1111状态的集合,他们的二进制表示都比1111小,所以一定会先遍历所有可到达的状态,进而能确保1111状态中记录的是最优情况。
可以把表示某一状态的数n,事先预处理n中已经有哪些元素加入,存入数组,就不用DP时每一位都算一遍了。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>#define INF 10000
#define ll long long
#define Pair pair<int,int>
#define re return#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;struct Cord{int x,y;
};
struct Tunnel{Cord star,finish;
};
struct Recd{int x,y;int len;
};int N,M;
int shif[5][2]={{0,1},{0,-1},{1,0},{-1,0}};
char store[18][18];
bool vis[20][20];
int dp[40000][15];
int dis[20][20];
vector<int> V[40000];
Tunnel tunnel[20];
int cal(Tunnel &a,Tunnel &b);
inline bool check(Cord &pos);
int main()
{rep(i,1,1<<15){rep(j,0,15){if(i & (1<<j))V[i].Push(j);}}while(scanf("%d %d",&N,&M)!=EOF){rep(i,1,N+1)scanf("%s",store[i]+1);rep(i,0,M){scanf("%d %d %d %d",&tunnel[i].star.x,&tunnel[i].star.y,&tunnel[i].finish.x,&tunnel[i].finish.y);}//cal disrep(i,0,M){rep(j,0,M){if(i==j){dis[i][j]=0;continue;}dis[i][j]=cal(tunnel[i],tunnel[j]);}}rep(i,1,1<<M){rep(j,0,M)dp[i][j]=INF;}rep(i,0,M){dp[1<<i][i]=0;}rep(i,1,1<<M){rep(x,0,getLen(V,i)){int j=V[i][x];int pre=i-(1<<j);rep(k,0,M){if(pre & (1<<k))dp[i][j]=min(dp[i][j],dp[pre][k]+dis[k][j]);}}}int ans=INF;rep(i,0,M){ans=min(ans,dp[(1<<M)-1][i]);}if(ans>=INF)ans=-1;printf("%d\n",ans);}return 0;
}
int cal(Tunnel &a,Tunnel &b){queue<Recd> Q;memset(vis,0,sizeof(vis));Q.push({a.finish.x,a.finish.y,0});vis[a.finish.x][a.finish.y]=true;while(!Q.empty()){Recd now=Q.front();Q.pop();if(now.x==b.star.x && now.y==b.star.y){return now.len;}Cord next;rep(i,0,4){next.x=now.x+shif[i][0],next.y=now.y+shif[i][1];if(!check(next) || vis[next.x][next.y] || store[next.x][next.y]=='#')continue;vis[next.x][next.y]=true;Q.push({next.x,next.y,now.len+1});}}return INF;
}
inline bool check(Cord &pos){if(pos.x>N || pos.x<=0 || pos.y>N || pos.y<=0){return false;}elsereturn true;
}

HDU4856 Tunnels 状压DP相关推荐

  1. Tunnels 状压DP+BFS

    Bob is travelling in Xi'an. He finds many secret tunnels beneath the city. In his eyes, the city is ...

  2. HDU-4856 Tunnels(BFS状压DP)

    Tunnels http://acm.hdu.edu.cn/showproblem.php?pid=4856 Time Limit: 3000/1500 MS (Java/Others)    Mem ...

  3. Tunnels HDU - 4856 (bfs状压dp)

    Tunnels HDU - 4856 Bob is travelling in Xi'an. He finds many secret tunnels beneath the city. In his ...

  4. HDU - 4856 Tunnels (预处理+状压dp)

    HDU - 4856 Tunnels (预处理+状压dp) [hud链接] [vj链接] 题目 Problem Description Bob is travelling in Xi'an. He f ...

  5. POJ 1038 Bugs Integrated Inc (复杂的状压DP)

    \(POJ~1038~~*Bugs~Integrated~Inc:\) (复杂的状压DP) \(solution:\) 很纠结的一道题目,写了大半天,就想练练手,结果这手生的.其实根据之前那道炮兵阵地 ...

  6. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  7. UVA10296 Jogging Trails(中国邮递员问题)(欧拉回路、一般图最大权匹配 / 状压DP)

    整理的算法模板合集: ACM模板 目录 思路 UVA10296 Jogging Trails 题目翻译: 给你n个点,m条无向边,每条边有一定的距离数值,构造成一个连通图.问从任意一点出发,遍历所有的 ...

  8. POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)

    poj 2411 Mondriaan's Dream(最清晰的状压DP解析) 闫氏DP大法好 我们这里是一列一列地来,因为是一个棋盘性的状态压缩DP,从哪个方向都一样 摆放的小方格总方案数 等价于 横 ...

  9. 【每日DP】day2、P1879 [USACO06NOV]Corn Fields G玉米地(状压DP模板题)难度⭐⭐⭐★

    昨天的每日DP我还在写01背包,今天就到状压DP了,真刺激. P1879 [USACO06NOV]Corn Fields G 题目链接 输入 2 3 1 1 1 0 1 0 输出 9 一道简单的状压D ...

最新文章

  1. 顶级数学家有多厉害?
  2. 在Ubuntu下解决E: 无法对目录 /var/lib/apt/lists/ 加锁的问题
  3. PHP结合Ueditor并修改图片上传路径
  4. 宝石世界1.0游戏发布
  5. 【数据结构与算法】之深入解析“路径交叉”的求解思路与算法示例
  6. Kubernetes集群安全概述
  7. uve (mui/light7)写APP的使用心得(大坑);
  8. android 图片上传图片 报Socket: Broken pipe
  9. 关于主机远程唤醒(WOL,Wake on Lan)的几种方法
  10. Leetcode之插入区间
  11. 专用集成电路设计实用教程(学习笔记一)
  12. 软件测试工程师工作必备模板五件套
  13. mysql不是内部命令_mysql不是内部命令的错误解决方案
  14. 正找工作的同学看过来——Java项目经验篇
  15. 14期-连肝7个晚上,总结了计算机网络的知识点!(共66条)
  16. 疯了!一个rm-rf把公司整个数据库删没了...
  17. 臻色调女装官网评价:专业人士展示行业数据
  18. Linux中SELinux理解
  19. 服务器主机型号,服务器的主机型号
  20. 计算机桌面怎么设置在开始菜单,电脑桌面开始菜单不见了应该如何解决?_开始菜单不见了解决办法介绍...

热门文章

  1. H5 应用封装成 ios app(PWA ),无需开发者账号。
  2. IIS10 部署网站报【HTTP 错误 500.19 - Internal Server Error】的解决办法
  3. 高中生如何攻克SAT词汇难关
  4. 【系统】VMware虚拟机安装Windows11
  5. Forest的快速入门以及源码解析
  6. python3 解决UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xcc in position 1448:
  7. 系统分析师每日练习错题知识点
  8. 医疗疾病感染数据分析——以手术感染为例
  9. JS中的12种循环遍历的方法
  10. JavaScript循环结构