• 【HDOJ】2757 Ocean Currents


    简单BFS。

      1 /* 2757 */
      2 #include <iostream>
      3 #include <queue>
      4 #include <cstdio>
      5 #include <cstring>
      6 #include <cstdlib>
      7 using namespace std;
      8 
      9 #define MAXN 1005
     10 
     11 typedef struct node_t {
     12     int k, t;
     13     node_t() {}
     14     node_t(int kk, int tt) {
     15         k = kk; t = tt;
     16     }
     17     friend bool operator <(const node_t &a, const node_t &b) {
     18         return a.t > b.t;
     19     }
     20 } node_t;
     21 
     22 int n, m;
     23 int bx, by, ex, ey;
     24 char map[MAXN][MAXN];
     25 int visit[MAXN][MAXN];
     26 int dir[8][2] = {
     27     -1,0, -1,1, 0,1, 1,1,
     28     1,0, 1,-1, 0,-1, -1,-1
     29 };
     30 
     31 inline bool check(int x, int y) {
     32     return x<0 || x>=n || y<=0 || y>=m;
     33 }
     34 
     35 int bfs() {
     36     int xx, yy;
     37     int x, y, t;
     38     int i, j, k;
     39     node_t nd;
     40     priority_queue<node_t> Q;
     41     
     42     memset(visit, 0x3f, sizeof(visit));
     43     visit[bx][by] = 0;
     44     nd.t = 0;
     45     nd.k = bx*1000+by;
     46     Q.push(nd);
     47     
     48     while (!Q.empty()) {
     49         nd = Q.top();
     50         xx = nd.k/1000;
     51         yy = nd.k%1000;
     52         if (xx==ex && yy==ey)
     53             return nd.t;
     54         Q.pop();
     55         for (i=0; i<8; ++i) {
     56             x = xx + dir[i][0];
     57             y = yy + dir[i][1];
     58             if (check(x, y))
     59                 continue;
     60             if (map[xx][yy] == i)
     61                 t = nd.t;
     62             else
     63                 t = nd.t + 1;
     64             if (visit[x][y] > t) {
     65                 visit[x][y] = t;
     66                 k = 1000*x+y;
     67                 Q.push(node_t(k, t));
     68             }
     69         }
     70     }
     71     
     72     return 0;
     73 }
     74 
     75 int main() {
     76     int i, j, k;
     77     int t;
     78     
     79     #ifndef ONLINE_JUDGE
     80         freopen("data.in", "r", stdin);
     81         freopen("data.out", "w", stdout);
     82     #endif
     83     
     84     while (scanf("%d%d",&n,&m) != EOF) {
     85         for(i=0; i<n; ++i) {
     86             scanf("%s", map[i]);
     87             for (j=0; j<m; ++j)
     88                 map[i][j] -= '0';
     89         }
     90         scanf("%d", &t);
     91         while (t--) {
     92             scanf("%d%d%d%d",&bx,&by,&ex,&ey);
     93             --bx; --by; --ex; --ey;
     94             if (bx==ex && by==ey)
     95                 k = 0;
     96             else
     97                 k = bfs();
     98             printf("%d
    ", k);
     99         }
    100     }
    101 
    102     return 0;
    103 }
  • 相关阅读:
    mongodb
    python中读取文件的read、readline、readlines方法区别
    uva 129 Krypton Factor
    hdu 4734
    hdu 5182 PM2.5
    hdu 5179 beautiful number
    hdu 5178 pairs
    hdu 5176 The Experience of Love
    hdu 5175 Misaki's Kiss again
    hdu 5174 Ferries Wheel
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4298796.html
Copyright © 2020-2023  润新知