Hdu4856:tunnels (BFS & pressure DP)

Source: Internet
Author: User
Tags exit in min

Bob is travelling in Xi ' an. He finds many secret tunnels beneath the city. In the He eyes, the city is a grid. He can ' t enter a grid with a barrier. In one minute, he can move to 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 had 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'll travel each of the tunnels once and only once. Now he wants to know, how long it would take him to visit all the tunnels (excluding the time when he was in the tunnels). Inputthe input contains mutiple testcases. Please process till EOF.
For each testcase, the first line contains the integers N (1≤n≤15), the side length of the square map and M (1≤m≤1 5), the number of tunnels.
The map of the city was given in the next N lines. Each line contains exactly N characters. Barrier is represented by "#" and empty grid are represented by ".".
Then M lines follow. Each line consists of four integers x 1, y 1, x 2, y 2, indicating there are 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 is both empty grid. Outputfor each case, output a integer indicating the minimal time Bob would use the total to walk between tunnels.
If It is impossible-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

Test instructions: A n*n diagram, there is an underground channel m, you can choose the starting point, requires the shortest distance of all channels, the channel has a starting point and the end of the S and T, from the Channel x T to channel y S to walk on the ground, on the ground can not cross the # lattice, each channel to walk once, and through a single channel distance

Train of thought: is the problem of traveling quotient, preprocessing the shortest distance from the end of each channel to the starting point of other channels, dp[i][j] means that the channel of the I state finally stops at the shortest distance of the J channel.

# include <bits/stdc++.h> # define PB push_back # define MP Make_pair # define INF 0x3f3f3f3f using namespace std;
Char s[20][20];
int n, M, dp[1<<15][20], dis[20][20], d[20][20];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
vector<int>mp[20][20]; struct Node {int A, B, C, D;}
G[20]; bool OK (int x, int y) {if (x<1| | y<1| | x>n| |
    Y>n) return false;
return true;
    } void BFs (int st) {memset (d,-1, sizeof (d));
    Queue<pair<int,int> >q;
    Q.push (MP (G[ST].C, G[ST].D));
    D[G[ST].C][G[ST].D] = 0;
        while (!q.empty ()) {int X=q.front (). First, Y=q.front (). Second;
        if (Mp[x][y].size ()) for (int ed:mp[x][y]) dis[st][ed] = D[x][y];
        Q.pop ();
            for (int i=0; i<4; ++i) {int px = x+dx[i];
            int py = Y+dy[i];
              if (ok (px, py) && s[px][py]!= ' # ' && d[px][py]==-1) {d[px][py] = d[x][y]+1;  Q.push (MP (px,py));
        }}}} int main () {while (~scanf ("%d%d", &n,&m)) {memset (DP, 0x3f, sizeof (DP));
        memset (DIS, 0x3f, sizeof (DIS));
        for (int i=0, i<=n; ++i) for (int j=0; j<=n; ++j) mp[i][j].clear ();
        for (int i=1; i<=n; ++i) scanf ("%s", s[i]+1);
            for (int i=0; i<m; ++i) {scanf ("%d%d%d%d", &AMP;G[I].A,&AMP;G[I].B,&AMP;G[I].C,&AMP;G[I].D);
        Mp[g[i].a][g[i].b].push_back (i);
        } for (int i=0; i<m; ++i) BFS (i);
        for (int i=0; i<m; ++i) dp[1<<i][i]=0; for (int i=1, i<1<<m; ++i) for (int j=0; j<m; ++j) if (! ( i& (1&LT;&LT;J))) for (int k=0; k<m; ++k) if (i& (1<<k)) dp[i| ( 1&LT;&LT;J)][j] = min (dp[i| (
        1&LT;&LT;J)][j], dp[i][k]+dis[k][j]);
        int ans = inf;
        for (int i=0; i<m; ++i) ans=min (ans, dp[(1<<m) -1][i]); printf ("%d\n ", ans!=inf?ans:-1);
} return 0;
 }

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.