zoukankan      html  css  js  c++  java
  • 【HDOJ】3329 The Flood

    超简单BFS。

      1 /* 3329 */
      2 #include <iostream>
      3 #include <queue>
      4 #include <cstdio>
      5 #include <cstring>
      6 #include <cstdlib>
      7 using namespace std;
      8 
      9 #define MAXN 105
     10 
     11 typedef struct node_t {
     12     int x, y;
     13     node_t() {}
     14     node_t(int xx, int yy) {
     15         x = xx; y = yy;
     16     }
     17 } node_t;
     18 
     19 int n, m, h, total;
     20 int map[MAXN][MAXN];
     21 bool visit[MAXN][MAXN];
     22 int dir[4][2] = {
     23     -1,0,1,0,0,-1,0,1
     24 };
     25 
     26 inline bool check(int x, int y) {
     27     return x<0 || x>=n || y<0 || y>=m;
     28 }
     29 
     30 void border_bfs(int x, int y) {
     31     int i, j, k;
     32     int xx, yy;
     33     queue<node_t> Q;
     34     node_t nd;
     35     
     36     Q.push(node_t(x, y));
     37     visit[x][y] = true;
     38     
     39     while (!Q.empty()) {
     40         nd = Q.front();
     41         Q.pop();
     42         --total;
     43         for (i=0; i<4; ++i) {
     44             xx = nd.x + dir[i][0];
     45             yy = nd.y + dir[i][1];
     46             if (check(xx, yy) || visit[xx][yy] || map[xx][yy]>h)
     47                 continue;
     48             visit[xx][yy] = true;
     49             Q.push(node_t(xx, yy));
     50         }
     51     }
     52 }
     53 
     54 void bfs(int x, int y) {
     55     int xx, yy, s;
     56     int i, j, k;
     57     queue<node_t> Q;
     58     node_t nd;
     59     
     60     Q.push(node_t(x, y));
     61     visit[x][y] = true;
     62     
     63     while (!Q.empty()) {
     64         nd = Q.front();
     65         Q.pop();
     66         --total;
     67         for (i=0; i<4; ++i) {
     68             xx = nd.x + dir[i][0];
     69             yy = nd.y + dir[i][1];
     70             if (check(xx,yy) || visit[xx][yy])
     71                 continue;
     72             visit[xx][yy] = true;
     73             Q.push(node_t(xx, yy));
     74         }
     75     }
     76 }
     77 
     78 int main() {
     79     int t = 0;
     80     int i, j, k, tmp;
     81     bool flag;
     82     int maxh;
     83     
     84     #ifndef ONLINE_JUDGE
     85         freopen("data.in", "r", stdin);
     86         freopen("data.out", "w", stdout);
     87     #endif
     88     
     89     while (scanf("%d %d",&n,&m)!=EOF && (n||m)) {
     90         maxh = -1;
     91         for (i=0; i<n; ++i) {
     92             for (j=0; j<m; ++j) {
     93                 scanf("%d", &map[i][j]);
     94                 if (map[i][j] > maxh)
     95                     maxh = map[i][j];
     96             }
     97         }
     98         tmp = n*m;
     99         for (h=1; h<maxh; ++h) {
    100             // handle border of map
    101             total = tmp;
    102             memset(visit, false, sizeof(visit));
    103             for (j=0; j<m; ++j) {
    104                 if (!visit[0][j] && map[0][j]<=h)
    105                     border_bfs(0, j);
    106                 if (!visit[n-1][j] && map[n-1][j]<=h)
    107                     border_bfs(n-1, j);
    108             }
    109             for (i=0; i<n; ++i) {
    110                 if (!visit[i][0] && map[i][0]<=h)
    111                     border_bfs(i, 0);
    112                 if (!visit[i][m-1] && map[i][m-1]<=h)
    113                     border_bfs(i, m-1);
    114             }
    115             // check if there exsits two or more lands
    116             flag = false;
    117             for (i=0; i<n; ++i) {
    118                 for (j=0; j<m; ++j) {
    119                     if (!visit[i][j]) {
    120                         flag = true;
    121                         break;
    122                     }
    123                 }
    124                 if (flag)
    125                     break;
    126             }
    127             if (flag) {
    128                 bfs(i, j);
    129                 if (total > 0)
    130                     break;
    131             }
    132         }
    133         if (h < maxh)
    134             printf("Case %d: Island splits when ocean rises %d feet.
    ", ++t, h);
    135         else
    136             printf("Case %d: Island never splits.
    ", ++t);
    137     }
    138     
    139     return 0;
    140 }
  • 相关阅读:
    计算系数
    P2734 [USACO3.3]游戏 A Game——区间dp+博弈论
    4.14作业
    安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据 2.制作jsp登录页面 index.jsp 提交到ok.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在 3.如果存在,把用户名保存,跳转到yes.jsp
    jsp 3.10作业
    软件测试第一次课堂练习3.4
    easysync 协同算法详解
    支付宝订阅消息推送
    Linux防火墙操作指令
    Windows端口号操作
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4307328.html
Copyright © 2011-2022 走看看