Tunnels HDU - 4856 (bfs状压dp)

版权声明: https://blog.csdn.net/nucleare/article/details/88595227

Tunnels

 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
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int N = 100007;
const int INF = 0x3f3f3f3f;
char g[33][33]; //地图15*15
int n; //n*n
int m; //通道 
struct Node {
	int x, y;
}; //坐标点 
struct NOde {
	Node st, en;
} node[N]; //通道两端点的坐标 
int dis[20][20]; //存出口到入口的最短距离 
int tmp[20][20]; //临时存放某个点到其它点的距离 //标记 
int dx[] = {0, 1, 0, -1}; //方向 
int dy[] = {1, 0, -1, 0};
void bfs(int s) { //起点 
	queue<Node> q;
	q.push(Node{node[s].en.x, node[s].en.y});
	memset(tmp, -1, sizeof(tmp));
	tmp[node[s].en.x][node[s].en.y] = 0; 
	while (!q.empty()) {
		Node now = q.front(); q.pop();
		for (int i = 0; i < 4; ++i) {
			int xx = now.x + dx[i];
			int yy = now.y + dy[i];
			if (xx < 1 || xx > n || yy < 1 || yy > n || g[xx][yy] == '#' || tmp[xx][yy] != -1) {
				continue;
			}
			tmp[xx][yy] = tmp[now.x][now.y] + 1; //更新距离
			q.push(Node{xx, yy}); 
		}
	} 
}
int in[33];
int ans = INF;
void dfs(int u, int dist) { ///暴力tle 超时了了了 
	int flag = 0;
	for (int i = 1; i <= n; ++i) {
		if (!in[i] && dis[u][i] < INF) {
			flag = 1;
			if (dist + dis[u][i] >= ans) continue; //剪枝 
			in[i] = 1;
			dfs(i, dist + dis[u][i]);
			in[i] = 0;
		}
	}
	if (!flag) {
		ans = min(ans, dist);
	}
}
//最多15个通道 
int dp[1<<15][16]; // dp[i][j]: i二进制表示每个点的到达状态,j为当前所在点 
//状态可能是一个以到达点的集合 
int main() {
	//深刻教训:需要用到状压dp的数组还是0~m比1~m好,方便二级制移位 
	
	while (~scanf ("%d %d", &n, &m)) {
		for (int i = 1; i <= n; ++i) {
			scanf ("%s", g[i]+1);
		}
		for (int i = 0; i < m; ++i) {
			scanf ("%d %d %d %d", &node[i].st.x, &node[i].st.y, &node[i].en.x, &node[i].en.y);
		}
		memset(dis, 0x3f, sizeof(dis));
		for (int i = 0; i < m; ++i) { //求出任意通道出口到其他入口的距离 
			bfs(i);
			for (int j = 0; j < m; ++j) {
				if (i == j) {
					dis[i][j] = 0; //通道两端距离为零 
					continue;
				}
				dis[i][j] = tmp[node[j].st.x][node[j].st.y]; 
				if (dis[i][j] == -1) dis[i][j] = INF; //!!! wawawawawawawawa
			}
		} 
		//状态转移方程:dp[i|(1<<k)][j] = min(dp[i|(1<<k)][j], dp[i][j] + dis[j][k])
		//!!!!! 
		memset(dp, 0x3f, sizeof(dp));
		for (int i = 0; i < m; ++i) dp[1<<i][i] = 0; //自己到自己的距离为零 
		for (int i = 1, M = 1<<m; i < M; ++i) { //0000~1111
			for (int j = 0; j < m; ++j) {
				if (!(i&(1<<j))) { //如果这状态第j个通道没来过 
					for (int k = 0; k < m; ++k) {
						if (i&(1<<k)) {
							dp[i|(1<<j)][j] = min(dp[i|(1<<j)][j], dp[i][k] + dis[k][j]);
							//printf ("ijk: %d %d %d\n", i, j, k);
							//printf ("%d %d %d\n", dp[i|(1<<(j-1))][j], dp[i][k], dis[k][j]);
						}
					}
				}
			}
		}
		ans = INF; 
		for (int i = 0; i < m; ++i) {
			ans = min(ans, dp[(1<<m)-1][i]);
		}
		if (ans == INF) {
			puts("-1");
		}  else {
			printf ("%d\n", ans);
		}
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nucleare/article/details/88595227
今日推荐