HDU 4856 tunnels

Source: Internet
Author: User
Tags exit in
Tunnelstime limit: 1500 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 4856
64-bit integer Io format: % i64d Java class name: Main Bob is traveling in Xi'an. he finds eclipsecret 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 ). inputthe 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. outputfor 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
Source2014 Xi'an national invitational competition: Pressure DP. 1 <(J-1) indicates the I-th tunnel is selected.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 20;18 struct Tunnels {19     int u,v,x,y;20 };21 Tunnels e[maxn];22 int n,m,g[maxn][maxn],dp[1<<15][maxn];23 char mp[maxn][maxn];24 int bfs(Tunnels &a,const Tunnels &b) {25     queue< pii >q;26     static int vis[maxn][maxn];27     static const int dir[4][2] = {0,-1,-1,0,1,0,0,1};28     memset(vis,-1,sizeof(vis));29     q.push(make_pair(a.x,a.y));30     vis[a.x][a.y] = 0;31     while(!q.empty()) {32         pii now = q.front();33         q.pop();34         if(now.first == b.u && now.second == b.v)35             return vis[now.first][now.second];36         for(int i = 0; i < 4; i++) {37             int x = now.first+dir[i][0];38             int y = now.second+dir[i][1];39             if(vis[x][y] > -1 || mp[x][y] == ‘#‘) continue;40             vis[x][y] = vis[now.first][now.second]+1;41             q.push(make_pair(x,y));42         }43     }44     return -1;45 }46 int main() {47     while(~scanf("%d %d",&n,&m)) {48         memset(mp,‘#‘,sizeof(mp));49         for(int i = 1; i <= n; i++)50             scanf("%s",mp[i]+1);51         for(int i = 1; i <= m; i++)52             scanf("%d %d %d %d",&e[i].u,&e[i].v,&e[i].x,&e[i].y);53         for(int i = 1; i <= m; i++) {54             for(int j = 1; j <= m; j++)55                 g[i][j] = bfs(e[i],e[j]);56         }57         memset(dp,INF,sizeof(dp));58         for(int i = 1; i <= m; i++) dp[1<<(i-1)][i] = 0;59         int ans = INF;60         for(int i = 1,M = 1 << m; i < M; i++) {61             for(int j = 1; j <= m; j++) {62                 if(i&(1<<(j-1))) {63                     for(int k = 1; k <= m; k++) {64                         if(k == j || (i&(1<<(k-1)) == 0) || g[k][j] == -1) continue;65                         dp[i][j] = min(dp[i][j],dp[i^(1<<(j-1))][k]+g[k][j]);66                     }67                 }68                 if(i == M-1) ans = min(ans,dp[i][j]);69             }70         }71         printf("%d\n",ans == INF?-1:ans);72     }73     return 0;74 }
View code

 

HDU 4856 tunnels

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.