蓝桥集训之山峰和山谷

蓝桥集训之山峰和山谷

  • 核心思想:Flood Fill

    • 多加两个bool变量 记录连通块周围是否有比它大/小的数
  •   #include <cstring>
      #include <iostream>
      #include <algorithm>
      
      #define x first
      #define y second
      
      using namespace std;
      typedef pair<int,int> PII;
      const int N = 1010,M = N*N;
      
      PII p[M];
      int h[N][N];
      bool st[N][N];
      int n;
      
      void bfs(int x,int y,bool &has_peak,bool &has_valley)
      {
          st[x][y] = true;
          p[0] = {x,y};
          int hh=0,tt=0;
          
          while(hh<=tt)
          {
              auto t = p[hh++];
              int a = t.first , b = t.second;
              for(int i=a-1;i<=a+1;i++)
              {
                  for(int j=b-1;j<=b+1;j++)
                  {
                      if (i < 0 || i >= n || j < 0 || j >= n) continue;
                      if(h[i][j] != h[a][b])
                      {
                          if(h[i][j] > h[a][b]) has_peak = true;
                          else has_valley = true;
                      }
                      else if(!st[i][j])
                      {
                          p[++tt] = {i,j};
                          st[i][j] = true;
                      }
                  }
              }
          }
      }
      int main()
      {
          cin>>n;
          for(int i=0;i<n;i++)
              for(int j=0;j<n;j++)
                  cin>>h[i][j];
                  
          int peak = 0,valley = 0;
          for(int i=0;i<n;i++)
          {
              for(int j=0;j<n;j++) 
              {
                  if(!st[i][j])
                  {
                      bool has_peak = false,has_valley = false;
                      bfs(i,j,has_peak,has_valley);
                      if(!has_valley) valley ++;
                      if(!has_peak) peak++;
                  }
              }
          }
          cout<<peak<<" "<<valley<<endl;
      }
    

相关推荐

  1. 集训山峰山谷

    2024-03-22 01:16:03       22 阅读
  2. 集训星空

    2024-03-22 01:16:03       19 阅读
  3. 集训日期差值

    2024-03-22 01:16:03       27 阅读
  4. 集训日期问题

    2024-03-22 01:16:03       23 阅读
  5. 集训奶牛选美

    2024-03-22 01:16:03       17 阅读
  6. 集训八数码

    2024-03-22 01:16:03       21 阅读
  7. 集训格子游戏

    2024-03-22 01:16:03       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 01:16:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 01:16:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 01:16:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 01:16:03       20 阅读

热门阅读

  1. 客户端渲染与服务端渲染(1)

    2024-03-22 01:16:03       15 阅读
  2. # termux连接云服务器

    2024-03-22 01:16:03       19 阅读
  3. ES查询小技能

    2024-03-22 01:16:03       19 阅读
  4. Python之Flask框架~消息闪现

    2024-03-22 01:16:03       17 阅读
  5. 使用Pytesseract进行OCR

    2024-03-22 01:16:03       19 阅读
  6. Mysql2-sql语句

    2024-03-22 01:16:03       20 阅读
  7. Oracle锁表解决方案

    2024-03-22 01:16:03       18 阅读
  8. Springboot 集成kafka 以及连接 带有SASL/PLAIN 的kafka

    2024-03-22 01:16:03       17 阅读