HDOJ 题目4856 Tunnels(BFS+状态压缩,TSP)

Tunnels

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


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   |   We have carefully selected several similar problems for you:   5498  5497  5495  5494  5493 
 题目大意:给你一个n,地图n*n的,#不能到,.可以到,可以上下左右走,每走一部消耗1点疲劳,下边m条通道,穿过通道不消耗疲劳,问把所有通道穿一遍,消耗最少的疲劳是多少
ac代码
150327042015-10-07 12:54:48Accepted4856499MS10300K1796 BG++Who_you?
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define INF 0x3f3f3f3f
using namespace std;
int dp[1<<17][17],dis[17][17],dist[17][17];
int sx[17],sy[17],ex[17],ey[17],vis[17][17];
int n,m;
char map[17][17];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct s
{
	int x,y,step;
	friend bool operator < (s a, s b)
	{
		return a.step>b.step;
	}
}a,temp;
void bfs(int xx)
{
	priority_queue<struct s>q;
	memset(vis,0,sizeof(vis));
	a.x=ex[xx];
	a.y=ey[xx];
	a.step=0;
	dis[a.x][a.y]=0;
	vis[a.x][a.y]=1;
	q.push(a);
	int i;
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		dis[a.x][a.y]=a.step;
		for(i=0;i<4;i++)
		{
			temp.x=a.x+dx[i];
			temp.y=a.y+dy[i];
			temp.step=a.step+1;
			if(map[temp.x][temp.y]=='.'&&!vis[temp.x][temp.y])
			{
				vis[temp.x][temp.y]=1;
				q.push(temp);
			}
		}
	}
	for(i=0;i<m;i++)
	{
		if(vis[sx[i]][sy[i]])
		{
			dist[xx][i]=dis[sx[i]][sy[i]];
		}
	}
	dist[xx][xx]=0;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j,k;
		memset(dp,INF,sizeof(dp));
		memset(dist,INF,sizeof(dist));
		for(i=1;i<=n;i++)
		{
			scanf("%s",map[i]+1);
		}
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d%d",&sx[i],&sy[i],&ex[i],&ey[i]);
		}
		for(i=0;i<m;i++)
		{
			bfs(i);
		}
		for(i=0;i<m;i++)//
			dp[1<<i][i]=0;
		int rt=(1<<m)-1;
		for(i=0;i<=rt;i++)
		{
			for(j=0;j<m;j++)
			{
				if(dp[i][j]==INF)
					continue;
				for(k=0;k<m;k++)
				{
					if(dist[j][k]==INF||i&(1<<k))
						continue;
					int st=i|(1<<k);
					dp[st][k]=min(dp[st][k],dp[i][j]+dist[j][k]);
				}
			}
		}
		int ans=INF;
		for(i=0;i<m;i++)
		{
			if(ans>dp[rt][i])
				ans=dp[rt][i];
		}
		if(ans==INF)
			ans=-1;
		printf("%d\n",ans);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值