Hdu5094Maze (BFS)

Source: Internet
Author: User
Link to the question: hdu5094Maze: Given a graph, you need to move from () to (n, m), and some of them have doors or walls in the middle. The corresponding door must have the corresponding key to pass through. And specify the location of the key. Solution: it is a common bfs. the ownership of the key can be represented by a binary number. The only trouble is that the door and wall are on the edge, so I am working on the edge directly during preprocessing. #

Link: hdu 5094 Maze

For a given graph, we need to move from () to (n, m). some of them have doors or walls in the middle. The corresponding door needs to be matched

. And specify the location of the key.

Solution: it is a common bfs. the ownership of the key can be represented by a binary number. The only trouble is that the door and wall are on the edge.

It is processed directly on the edge.

#include 
  
   #include 
   
    #include 
    
     #include 
     
      #include using namespace std;typedef pair
      
        pii;const int maxn = 55;const int maxs = 3000;const int inf = 0x3f3f3f3f;const int dir[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}};int N, M, P, v[maxn][maxn];int g[maxn * maxn][4], dp[maxn][maxn][maxs];inline int idx(int x, int y) { return (x-1) * M + y - 1;}void init () { memset(v, 0, sizeof(v)); memset(g, -1, sizeof(g)); memset(dp, inf, sizeof(dp)); int n, x1, y1, x2, y2, k, d; scanf("%d", &n); while (n--) { scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k); if (x1 == x2) d = (y1 > y2 ? 2 : 1); else d = (x1 > x2 ? 0 : 3); g[idx(x1, y1)][d] = g[idx(x2, y2)][3-d] = k; } scanf("%d", &n); while (n--) { scanf("%d%d%d", &x1, &y1, &k); v[x1][y1] |= (1<<(k-1)); }}int bfs () { queue
       
         Q; Q.push(make_pair(idx(1, 1), 0)); dp[1][1][0] = 0; while (!Q.empty()) { pii u = Q.front(); Q.pop(); int x = u.first / M + 1; int y = u.first % M + 1; if (x == N && y == M) return dp[x][y][u.second]; for (int i = 0; i < 4; i++) { if (g[u.first][i] == 0) continue; int xx = x + dir[i][0]; int yy = y + dir[i][1]; if (xx <= 0 || xx > N || yy <= 0 || y > M) continue; int s = u.second | v[xx][yy]; int tmp = g[u.first][i]; if (tmp == -1 || (u.second & (1<<(tmp-1)))) { if (dp[xx][yy][s] > dp[x][y][u.second] + 1) { dp[xx][yy][s] = dp[x][y][u.second] + 1; Q.push(make_pair(idx(xx, yy), s)); } } } } return -1;}int main () { while (scanf("%d%d%d", &N, &M, &P) == 3) { init(); printf("%d\n", bfs()); } 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.