HDU-4856 Tunnels (bfs+-like pressure DP)

Source: Internet
Author: User
Tags exit in

Problem Descriptionbob 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). Input the 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 x1, y1, x2, y2, indicating there are a tunnel with entrence in (x1, y1) and exit in (x2 , y2). It's guaranteed that (x1, y1) and (x2, y2) in the map is both empty grid. Output A For each case, output a integer indicating the minimal time Bob would use the total to walk between tunnels.
If It is a impossible for Bob to visit all the tunnels, Output-1. sample Input5 4....#...# ...... 2 3 1 2 3 1 sample 3 3 4 2 output7  title: In a picture of NxN (n<16), "." Represents an empty area, "#" represents a barrier, each move is in the direction of one or the other, time-consuming plus 1 per move. There is a tunnel of M-known imports and exits, and moving in the tunnel does not take time. Q. What is the minimum time required to visit all the tunnels? Order of choice. Topic Analysis: A look at the size of the data so small, I think it is a pressure DP. However, the distance between the 22 tunnels (the exit of the starting tunnel and the distance of the inlet of the terminating tunnel) should be pretreated with BFS. The definition state DP (I,J) represents the minimum time that has passed since the tunnel set is I, and is currently in the J position. The state transition equation is DP (i| ( 1<<k), K) =min (DP (i| ( 1<<k), K), DP (I,J) +dist (j,k)). The    code is as follows:
# include<iostream># include<cstdio># include<queue># include<cstring># include< algorithm>using namespace Std;const int inf=100000;struct tt{int sx,sy,ex,ey;};    struct node{int x,y,t; Node (int _x,int _y,int _t): X (_x), Y (_y), T (_t) {}};tt T[15];char mp[20][20];int dist[20][20];int vis[20][20],n,m,dp[1    <<15][15];int d[4][2]={{0,-1},{0,1},{-1,0},{1,0}};int bfs (int sx,int sy,int ex,int ey) {queue<node>q;    memset (vis,0,sizeof (VIS));    Vis[sx][sy]=1;    Q.push (Node (sx,sy,0));        while (!q.empty ()) {node U=q.front ();        Q.pop ();        if (U.x==ex&&u.y==ey) return u.t;            for (int i=0;i<4;++i) {int nx=u.x+d[i][0],ny=u.y+d[i][1]; if (nx>=1&&nx<=n&&ny>=1&&ny<=n&&!vis[nx][ny]&&mp[nx][ny]!= '                # ') {vis[nx][ny]=1;            Q.push (Node (nx,ny,u.t+1)); }}} return INF;} void Init () {for (int i=0;i<m;++i) for (int j=0;j<m;++j) DIST[I][J]=BFS (t[i].ex,t[i].ey,t[j].sx,t[j].sy);}    void DP () {int tot=1<<m;    for (int i=0;i<tot;++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 i=1;i<tot;++i) {for (int j=0;j<m;++j) {if (i& (1<<j)) continue; int sta=i|            (1&LT;&LT;J); for (int k=0;k<m;++k) {if (i& (1<<k)) Dp[sta][j]=min (dp[sta][j],dp[i][k]+dist[            K][J]);    }}} int ans=inf;    for (int i=0;i<m;++i) ans=min (Ans,dp[tot-1][i]);    if (ans==inf) printf (" -1\n"); else printf ("%d\n", ans);}        int main () {while (~scanf ("%d%d", &n,&m)) {for (int i=1;i<=n;++i) scanf ("%s", mp[i]+1);        for (int i=0;i<m;++i) scanf ("%d%d%d%d", &t[i].sx,&t[i].sy,&t[i].ex,&t[i].ey); Init ();       DP (); } return 0;}

  

HDU-4856 Tunnels (bfs+-like pressure DP)

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.