Tunnels (BFS+状压dp)

https://cn.vjudge.net/problem/HDU-4856

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 x 1, y 1, x 2, y 2, indicating there is a tunnel with entrence in (x 1, y 1) and exit in (x 2, y 2). It’s guaranteed that (x 1, y 1) and (x 2, y 2) 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

 题意:给你一个n*n的地图,M个隧道,给出隧道的出口和入口的坐标,问遍历所有隧道最少的花费

思路:把每个隧道看做一个点,把每个隧道终点到其他隧道起点的最短距离作为隧道点之间的距离(BFS),然后就是TSP问题了,状压dp

坑点:起点可能有重合,终点也可能有重合

好久没写bfs了,嘤嘤嘤

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int N=20;
const int INF=0x3f3f3f3f;
int n,m;
struct node{
	int x,y,step;
}p,id[N];
vector<node>v;
char mp[N][N];
int dis[N][N];
int Next[4][2]={0,1,0,-1,1,0,-1,0};
int vis[N][N];
void bfs(int k){
	memset(vis,-1,sizeof(vis));
	queue<node> q;
	q.push(v[k]);
	vis[v[k].x][v[k].y]=0;
	while(!q.empty()){
		p=q.front();
		q.pop();
		int x=p.x,y=p.y,step=p.step+1;
		for(int i=0;i<4;i++){
			p.x=x+Next[i][0],p.y=y+Next[i][1];
			if(p.x>=1&&p.x<=n&&p.y>=1&&p.y<=n&&vis[p.x][p.y]==-1&&mp[p.x][p.y]!='#'){
				p.step=step;
				q.push(p);
				vis[p.x][p.y]=p.step;
			}
		}
	}
	for(int i=0;i<m;i++){
		int x=id[i].x,y=id[i].y;
		if(vis[x][y]!=-1) dis[k][i]=vis[x][y];
	}
}
int dp[1<<15][15];
int main(){
	while(~scanf("%d%d",&n,&m)){
		v.clear();
		for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
		for(int i=0;i<m;i++){
			int sx,sy,ex,ey;
			scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
			v.push_back(node{ex,ey,0});
			id[i]={sx,sy,0};
		}
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				if(i==j) dis[i][j]=0;
				else dis[i][j]=-1;
			}
		}
		for(int i=0;i<m;i++) bfs(i);
		int up=1<<m;
		for(int i=0;i<up;i++){
			for(int j=0;j<m;j++){
				dp[i][j]=INF;
			}
		}
		for(int i=0;i<m;i++){
			dp[1<<i][i]=0;
		}
		for(int i=0;i<up;i++){
			for(int j=0;j<m;j++){
				if(dp[i][j]==INF) continue;
				for(int k=0;k<m;k++){
					if(i&(1<<k)) continue;
					if(dis[j][k]==-1) continue;
					dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dis[j][k]);
				}
			}
		}
		int ans=INF;
		for(int i=0;i<m;i++) ans=min(ans,dp[up-1][i]);
		if(ans==INF) printf("-1\n");
		else printf("%d\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值