hdu 4856 Tunnels dp+bfs+状压

Tunnels

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


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
 


链接:http://acm.hdu.edu.cn/showproblem.php?pid=4856


题意:给你一个n*n地图,再给你m个有向通道,每个通道走的时间为0,走格子的时间为1,问最短多少时间走完所有通道,所有通道要求走  有且仅有一次 。


做法:

dis[i][j] 代表i的通道出口到j的通道入口要走多少步。

dp[i][j],i是二进制代表每个通道走过没有,j代表最后一个走的通道。dp值为完成这个状态最少的步数。

dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dis[j][k]);这个是转移公式,从j通道的出口到k通道的入口

然后更新一遍,把i==(1<<m)-1的 所有最后所在地遍历一遍,求个最小值就行了。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <sstream>
#define eps 0.00001
#define LL __int64
#define pi acos(-1.0)

#define INF 0x1f1f1f1f1f
int n,m;
struct lujin
{
	int x1,y1,x2,y2;
};

lujin pkk[20];

char mp[20][20];

struct node
{
	int x,y;
	int bu;
};
node dian;

int ok(int xx,int yy)
{
	if(xx<0||xx>=n||yy<0||yy>=n||mp[xx][yy]=='#')
		return 0;
	return 1;
}

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int viss[20][20];
int len(int i,int j)//i终点 j起点
{
	queue <node> q;
	node nw,sta,ee,nxt;
	sta.x=pkk[i].x2,sta.y=pkk[i].y2;
	sta.bu=0;
	ee.x=pkk[j].x1,ee.y=pkk[j].y1;
	ee.bu=0;
	q.push(sta);
    memset(viss,0,sizeof viss);
	viss[sta.x][sta.y]=1;
	while(!q.empty())
	{
		nw=q.front();
		q.pop();

		if(nw.x==ee.x&&nw.y==ee.y)
			return nw.bu;
		for(int i=0;i<4;i++)
		{
			int xx=nw.x+dir[i][0];
			int yy=nw.y+dir[i][1];
			nxt.x=xx;
			nxt.y=yy;
			nxt.bu=nw.bu+1;

			if(ok(xx,yy)&&viss[xx][yy]==0)
			{
			    viss[xx][yy]=1;
				q.push(nxt);
			}
		}
	}
	return INF;
}

int dp[1<<16][16];//这个状态 最后一个到达点
int dis[20][20];
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%s",mp[i]);

		for(int i=0;i<m;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y); 
			pkk[i].x1=x-1;
			pkk[i].y1=y-1;
			scanf("%d%d",&x,&y); 
			pkk[i].x2=x-1;
			pkk[i].y2=y-1;
		}

		if(m==1)
		{
			printf("%d\n",0);
			continue;
		}

		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
				dis[i][j]=len(i,j);//i尾 到j头
		}

		memset(dp,0x1f1f1f1f1f,sizeof dp); 
		for(int i=0;i<m;i++)
			dp[1<<i][i]=0;  
		for(int i=1;i<(1<<m)-1;i++)
		{
			for(int j=0;j<m;j++)//最后一个点
			{
				if((i&(1<<j)))
				for(int k=0;k<m;k++)//tou 
				{
					if((i&(1<<k))==0)//还没有走过
						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[(1<<m)-1][i]);
		} 
		int ttt=INF;
		if(ans==ttt)
			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、付费专栏及课程。

余额充值