HDU 4856 tunnels (BFS + pressure DP)

Source: Internet
Author: User

Question Link

Question:

A square grid graph with a side length of N, with some vertices '. 'indicates reachable,' # 'indicates inaccessible. You cannot go to an inaccessible point, and you can only go to an adjacent grid (up, down, and left) per unit of time ). Now we will give you m channels, starting positions (x1, Y1) and ending points (X2, Y2) of each channel ), when you get in from the starting point, you can immediately get out of the ending point (no time is spent), but you can only go through each secret path. Now, you can select any reachable point as the starting point and ask if you can finish all the secrets if the conditions are met. If there is a solution to output the shortest time, otherwise-1 is output.

Analysis:

The distance between each tunnel is first processed by BFs, and then the order between the tunnels is followed. The pressure DP can be used for processing,

DP [I] [J] indicates that the passed channel status is I (I indicates the compression status, and each bit of binary indicates whether the corresponding channel has passed, and the pass is 1, otherwise, it is 0), and the last pass is the minimum time when J is used.

The state transition equation is: DP [I | (1 <k)] [k] = min {DP [I | (1 <k)] [K], DP [I] [J] + dist [J] [k]}.
Ensure that the DP [I] [J] is in a state. Dist [J] [k] is not Inf (J can reach K ), and status I does not include the K th secret channel before transfer.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <queue> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 #define LL __int64 9 const int maxn = 20+10;10 using namespace std;11 const int INF = 1<<28;12 char s[maxn][maxn];13 int c[maxn][maxn], d[1<<17][20], n;14 int dx[] = {0, 0, 1, -1};15 int dy[] = {1, -1, 0, 0};16 struct node17 {18     int x, y, step;19 }pos, ne;20 int bfs(int a, int b, int c, int d)21 {22     queue<node>q;23     int vis[maxn][maxn], i;24     memset(vis, 0, sizeof(vis));25     ne.x = a; ne.y = b; ne.step = 0;26     vis[a][b] = 1;27     q.push(ne);28     while(!q.empty())29     {30         pos = q.front();31         q.pop();32         if(pos.x == c && pos.y == d)33         return pos.step;34         for(i = 0; i < 4; i++)35         {36             ne.x = pos.x+dx[i];37             ne.y = pos.y+dy[i];38             ne.step = pos.step+1;39             if(!(ne.x>=1&&ne.x<=n && ne.y>=1&&ne.y<=n)) continue;40             if(s[ne.x][ne.y]==‘#‘) continue;41             if(vis[ne.x][ne.y]) continue;42             q.push(ne);43             vis[ne.x][ne.y] = 1;44         }45     }46     return INF;47 }48 int main()49 {50     int m, i, j, k, ans;51     int x1[maxn], y1[maxn], x2[maxn], y2[maxn];52     while(~scanf("%d%d", &n, &m))53     {54         memset(c, 0, sizeof(c));55         memset(s, 0, sizeof(s));56         for(i = 1; i <= n; i++)57         {58             getchar();59             for(j = 1; j <= n; j++)60             scanf("%c", &s[i][j]);61         }62         for(i = 0; i < m; i++)63          cin>>x1[i]>>y1[i]>>x2[i]>>y2[i];64         for(i = 0; i < m; i++)65         for(j = 0; j < m; j++)66         {67             if(i == j) continue;68             c[i][j] = bfs(x2[i], y2[i], x1[j], y1[j]);69         }70         for(i = 0; i < (1<<m); i++)71         for(j = 0; j < m; j++)72         d[i][j] = INF;73 74         for(i = 0; i < m; i++)75         d[1<<i][i] = 0;76 77         for(i = 0; i < (1<<m); i++)78         for(j = 0; j < m; j++)79         if(d[i][j]!=INF)80         for(k = 0; k < m; k++)81         {82             if(!(i&(1<<k)) && c[j][k]!=INF)83             d[i|(1<<k)][k] = min(d[i|(1<<k)][k], d[i][j]+c[j][k]);84         }85         ans = INF;86         for(i = 0; i < m; i++)87         ans = min(ans, d[(1<<m)-1][i]);88         if(ans == INF) printf("-1\n");89         else90         printf("%d\n", ans);91     }92     return 0;93 }

 

HDU 4856 tunnels (BFS + 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.