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

来源:互联网 发布:ntfs for mac osx9.5 编辑:程序博客网 时间:2024/06/06 07:28

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 x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y1) and (x2, y2) 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 41 2 3 52 3 3 15 4 2 1
 

Sample Output
7
 

Source
2014西安全国邀请赛
 

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 0x3f3f3f3fusing 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);}}


0 0
原创粉丝点击