HDU 4856 Tunnels(BFS+状压DP)

Tunnels

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1859    Accepted Submission(s): 546


Problem Description
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
 

Source
 

Recommend
liuyiding
 

题意:给出一张N*N的地图,然后有M个隧道的入口和出口,从入口到出口的时间不计,然后要你求经过所以的隧道一次仅一次的最短时间。

思路:首先用BFS求出隧道与隧道之间的最短时间(即一个隧道的出口到另一隧道的入口),然后就是一个不会到起点的TSP模板了。

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <stack>
#include <cmath>
#include <iomanip>
using namespace std;
#define inf 0x3f3f3f3f
#define CLR(x) memset(x,0,sizeof(x))
#define MEM(x) memset(x,inf,sizeof(x))
typedef long long ll;
int dp[1<<15][25];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int n,m;
int vis[25][25],cnt[25][25],dist[25][25];
char Map[25][25];
struct edge
{
	int x,y;
	int step;
};
int bfs(edge start, edge end)//求出隧道的出口到另一隧道的入口的最短时间
{
	if(start.x == end.x &&  start.y == end.y) return 0;
	CLR(vis);
	queue<edge>Q;
	start.step = 0;
	vis[start.x][start.y] = 1;
	Q.push(start);
	edge head,next;
	while (!Q.empty())
	{
		head = Q.front();
		Q.pop();
		if(head.x == end.x && head.y == end.y)return head.step;
		for(int i =0; i< 4; i++)
		{
			next.x = head.x + dir[i][0];
			next.y = head.y + dir[i][1];
			if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < n && !vis[next.x][next.y] && Map[next.x][next.y] == '.')
			{
				next.step = head.step + 1;
				vis[next.x][next.y] = 1;
				Q.push(next);
			}
		}
	}
	return inf;
}
int main()
{
#ifdef CDZSC_June
	freopen("t.txt","r",stdin);
#endif
	while(~scanf("%d %d",&n,&m))
	{
		for(int i = 0; i < n;i++)
		{
			scanf("%s",Map[i]);
		}
		edge start[20],end[20];
		for(int i = 0; i < m;i++)
		{
			scanf("%d %d %d %d",&start[i].x,&start[i].y,&end[i].x,&end[i].y);
			start[i].x--,start[i].y--,end[i].x--,end[i].y--;
		}
		for(int i = 0; i < m;i++)
		{
			for(int j = 0; j < m;j++)
			{
				if(i == j)
				{
					dist[i][j] = 0;
					continue;
				}
				dist[i][j] = bfs(end[i],start[j]);
			}
		}
		MEM(dp);
		for(int i = 0; i < m;i++)
		{
			dp[1<<i][i] = 0;
		}
		int tmp = 1<<m;
		for(int s = 0; s < tmp; s++)//某一状态的枚举
		{
			for(int v = 0; v< m; v++)//当前到达的那个点
			{
				for(int u = 0; u<m; u++)//枚举v(当前到达的那个点)能到达哪个点
				{
					if(!((s >> u) & 1))//判断这个点有木有被访问过了
					{
						dp[s | 1<<u][u] = min(dp[s][v] + dist[v][u],dp[s | 1<<u][u]);
					}
				}
			}
		}
		int min1 = inf;
		for(int i = 0;i < m; i++)
		{
			min1 = min(min1,dp[tmp-1][i]);
		}
		if(min1 == inf)
			printf("-1\n");
		else
			printf("%d\n",min1);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值