Tunnels
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1460 Accepted Submission(s): 435
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 4
1 2 3 5
2 3 3 1
5 4 2 1
Sample Output
7
Source
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; #define INF 100000000 int n,m,dist[20][20],dp[(1<<15)+5][20]; char s[20][20]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; bool vis[20][20]; struct node { int sx,sy,ex,ey; }e[20]; struct pos { int x,y,step; }; int bfs(node a,node b) { queue<pos> q; memset(vis,0,sizeof(vis)); pos fr,nt; fr.x=a.ex,fr.y=a.ey,fr.step=0; vis[fr.x][fr.y]=1; q.push(fr); while(!q.empty()) { fr=q.front(),q.pop(); if(fr.x==b.sx&&fr.y==b.sy) { return fr.step; } for(int i=0;i<4;i++) { nt.x=fr.x+dx[i]; nt.y=fr.y+dy[i]; if(nt.x<0||nt.y<0||nt.x>=n||nt.y>=n||vis[nt.x][nt.y]||s[nt.x][nt.y]==‘#‘) continue; vis[nt.x][nt.y]=1; nt.step=fr.step+1; q.push(nt); } } return -1; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<n;i++) { scanf("%s",s[i]); } for(int i=0;i<m;i++) { scanf("%d%d%d%d",&e[i].sx,&e[i].sy,&e[i].ex,&e[i].ey); e[i].sx--,e[i].sy--,e[i].ex--,e[i].ey--; } 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(e[i],e[j]); if(dist[i][j]==-1) dist[i][j]=INF; } } for(int i=0;i<(1<<m);i++) for(int j=0;j<m;j++) dp[i][j]=INF; for(int i=0;i<m;i++) dp[(1<<i)][i]=0; for(int s=0;s<(1<<m);s++) { for(int j=0;j<m;j++) { if(dp[s][j]==INF) continue; for(int k=0;k<m;k++) { if(s&(1<<k)) continue; if(dist[j][k]==INF) continue; dp[s|(1<<k)][k]=min(dp[s|(1<<k)][k],dp[s][j]+dist[j][k]); } } } int ans=INF; for(int i=0;i<m;i++) ans=min(ans,dp[(1<<m)-1][i]); if(ans>=INF) { printf("-1\n"); } else printf("%d\n",ans); } return 0; }