zoukankan      html  css  js  c++  java
  • 10324 Global Warming dfs + 二分

    时间限制:1000MS  内存限制:65535K
    提交次数:0 通过次数:0

    题型: 编程题   语言: G++;GCC

     

    Description

    Global warming is a big problem, isn't it? It is reported that the ice of Antarctica is melting. It results that oceans are glowing 
    and more and more places are flooded. Now the city of CODE and the city of ACM are in front of the problem though no wather seperate 
    the two cities. The situation will change with the ice continue melting. It is reported that the oceans glow P cm per year. The mayors 
    of  the two cities want to know how many yeas there is before the two cities are seperated by water.
    The map can be descriped as following
    It is a M*N blocks
    The city of CODE locates in the block of coordinate(1,1), the city of ACM locates in the block of coordinate(M,N). The altitude of 
    every blocks are given. It is guaranteed that the block of (1,1) and the block of (M,N) is higher than their neighbor block.
    The following map shows the two cities weren't seperated by water.
    The following map shows the two cities are eperated by water.




    输入格式

    The first line contains the two integer numbers N (1 <= N <= 500), M (1 <= M <= 500). Then N lines are following. 
    Each line contains 
    M integer numbers(seperated by a space). Each number no more than 1,000,000,000). The last line contains the integer number 
    P (1 <= P <= 1,000,000,000)



    输出格式

    The only line of the output contains the years mentioned above.



     

    输入样例

    3 3
    9 5 7
    4 6 8
    8 3 9
    1
    



     

    输出样例

    6


    思路:以图中的最低海拔作为low,最高海拔作为high,二分一个值k,该值为能淹没部分区域并且把(1,1)和(n,m) 隔开的下界,那么答案即为 k/p 取上界。

    其中用到dfs判断(1,1)和(n,m)两个点是否连通

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 using namespace std ;
     6 typedef long long ll ;
     7 const int INF = 1 << 25 ;
     8 ll r[505][505] ;
     9 int vis[505][505] ;
    10 int dir[][2] = {{-1, 0},{0, -1},{1, 0},{0, 1}} ;
    11 ll n, m, mx, mi, p ;
    12 void dfs(int i, int j)
    13 {
    14     vis[i][j] = 1 ;
    15     for(int k = 0; k < 4; ++k)
    16     {
    17         int ti = i + dir[k][0] ;
    18         int tj = j + dir[k][1] ;
    19         if(ti < 1 || ti > n || tj < 1 || tj > m) continue ;
    20         if(vis[ti][tj]) continue ;
    21         dfs(ti, tj) ;
    22     }
    23 }
    24 int go(int t)
    25 {
    26     memset(vis, 0, sizeof vis) ;
    27     for(int i = 1; i <= n; ++i)
    28         for(int j = 1; j <= m; ++j)
    29             if(r[i][j] < t) vis[i][j] = 1 ;
    30     dfs(1,1) ;
    31     if(vis[n][m]) return 0 ;
    32     else return 1 ;
    33 }
    34 int solve()
    35 {
    36     int low = mi, high = mx ;
    37     while(low < high)//二分
    38     {
    39 
    40         int m = (low + high) >> 1 ;
    41         //printf("[%d] ",m) ;
    42         if(go(m)) high = m ;
    43         else low = m + 1 ;
    44     }
    45     return low ;
    46 }
    47 int main()
    48 {
    49     #ifdef LOCAL
    50     freopen("in.txt","r",stdin) ;
    51     #endif
    52     mi = INF ; mx = -INF ;
    53     scanf("%d%d",&n,&m) ;
    54     for(int i = 1; i <= n; ++i)
    55     for(int j = 1; j <= m; ++j){
    56         scanf("%lld",&r[i][j]) ;
    57         if(r[i][j] > mx) mx = r[i][j] ;
    58         else if(r[i][j] < mi) mi = r[i][j] ;
    59     }
    60    // printf("%d %d
    ",mi,mx) ;
    61     scanf("%lld",&p) ;
    62     int ans ;
    63     ans = solve() ;
    64    // printf("%d
    ",ans) ;
    65     if(ans % p == 0) ans = ans / p ;
    66     else ans = ans / p + 1 ;
    67     printf("%d
    ",ans) ;
    68 }
    View Code
  • 相关阅读:
    Net分层架构
    JavaScript设计模式_01_单例模式
    JavaScript设计模式_04_迭代器模式
    JavaScript设计模式_03_代理模式
    JavaScript设计模式_02_策略模式
    eclipse Indigo Helios Galileo几种版本的意思
    转:Tomcat:IOException while loading persisted sessions: java.io.EOFException解决手记
    一台服务器多个TomCat设置
    转:Struts2如何获取request、session和application对象
    JAVA Swing中JTable的固定列问题
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/4533168.html
Copyright © 2011-2022 走看看