当前位置:   article > 正文

一、Flood Fill --- 联通块块数问题

联通块

活动 - AcWing (1097.池塘计数)
题意:找到以W点为起始的连通块

思路:多方向dfs,更新走过的点,统计联通块的数量

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define PII pair<int, int>
  5. #define PLL pair<ll, ll>
  6. const int N = 1010;
  7. const int M = 2 * N;
  8. const int INF = 0x3f3f3f3f;
  9. const int mod = 1e9 + 7;
  10. const double PI = acos(-1.0);
  11. const double eps = 1e-8;
  12. int n,m;
  13. int cnt;
  14. char mp[N][N];
  15. //int hr[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
  16. void dfs(int x,int y)
  17. {
  18. mp[x][y] = '.';
  19. for(int i = -1; i <= 1; i ++)
  20. for(int j = -1; j <= 1; j ++)
  21. {
  22. int a = x + i;
  23. int b = y + j;
  24. if(a < 0 || a >= n || b < 0 || b >= m) continue;
  25. if(mp[a][b] == '.') continue;
  26. dfs(a,b);
  27. }
  28. }
  29. void solve()
  30. {
  31. scanf("%d%d",&n,&m);
  32. for(int i = 0; i < n; i ++) scanf("%s",mp[i]);
  33. for(int i = 0; i < n; i ++)
  34. {
  35. for(int j = 0; j < m; j ++)
  36. {
  37. if(mp[i][j] == 'W')
  38. {
  39. dfs(i,j);
  40. cnt ++;
  41. }
  42. }
  43. }
  44. printf("%d\n",cnt);
  45. }
  46. int main()
  47. {
  48. solve();
  49. system("pause");
  50. return 0;
  51. }

 

活动 - AcWing(1098.城堡问题)

题意:找到连通块最大值和个数

思路:多方向bfs,更新走过的点,统计联通块的数量,并且更新最大值

注意 8421 在二进制更新条件下的方向数组要与对应方向一致的问题

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define PII pair<int, int>
  5. #define PLL pair<ll, ll>
  6. const int N = 1010;
  7. const int M = 2 * N;
  8. const int INF = 0x3f3f3f3f;
  9. const int mod = 1e9 + 7;
  10. const double PI = acos(-1.0);
  11. const double eps = 1e-8;
  12. int n,m;
  13. int cnt;
  14. char mp[N][N];
  15. //int hr[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
  16. void dfs(int x,int y)
  17. {
  18. mp[x][y] = '.';
  19. for(int i = -1; i <= 1; i ++)
  20. for(int j = -1; j <= 1; j ++)
  21. {
  22. int a = x + i;
  23. int b = y + j;
  24. if(a < 0 || a >= n || b < 0 || b >= m) continue;
  25. if(mp[a][b] == '.') continue;
  26. dfs(a,b);
  27. }
  28. }
  29. void solve()
  30. {
  31. scanf("%d%d",&n,&m);
  32. for(int i = 0; i < n; i ++) scanf("%s",mp[i]);
  33. for(int i = 0; i < n; i ++)
  34. {
  35. for(int j = 0; j < m; j ++)
  36. {
  37. if(mp[i][j] == 'W')
  38. {
  39. dfs(i,j);
  40. cnt ++;
  41. }
  42. }
  43. }
  44. printf("%d\n",cnt);
  45. }
  46. int main()
  47. {
  48. solve();
  49. system("pause");
  50. return 0;
  51. }

活动 - AcWing(1106. 山峰和山谷)

题意:找到八连通的最高的区块山和最矮的区块谷

思路:bfs爆搜即可

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define PII pair<int, int>
  5. #define PLL pair<ll, ll>
  6. const int N = 1010;
  7. const int M = 2 * N;
  8. const int INF = 0x3f3f3f3f;
  9. const int mod = 1e9 + 7;
  10. const double PI = acos(-1.0);
  11. const double eps = 1e-8;
  12. int n;
  13. int hr[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
  14. int g[N][N],st[N][N];
  15. void bfs(int x,int y,bool &high, bool &low)
  16. {
  17. int stand = g[x][y];
  18. st[x][y] = 1;
  19. queue<PII>q;
  20. q.push({x,y});
  21. while(q.size())
  22. {
  23. auto t = q.front();
  24. q.pop();
  25. for(int i = 0; i < 8; i ++)
  26. {
  27. int a = t.first + hr[i][0];
  28. int b = t.second + hr[i][1];
  29. if(a < 0 || a >= n || b < 0 || b >= n) continue;
  30. if(g[a][b] > stand) low = 1;
  31. if(g[a][b] < stand) high = 1;
  32. if(st[a][b]) continue;
  33. if(g[a][b] == stand)
  34. {
  35. q.push({a,b});
  36. st[a][b] = 1;
  37. }
  38. }
  39. }
  40. }
  41. void solve()
  42. {
  43. scanf("%d",&n);
  44. for(int i = 0; i < n; i ++)
  45. for(int j = 0; j < n; j ++)
  46. scanf("%d",&g[i][j]);
  47. int peak = 0,valley = 0;
  48. for(int i = 0; i < n; i ++)
  49. for(int j = 0; j < n; j ++)
  50. if(!st[i][j])
  51. {
  52. bool high = 0,low = 0;
  53. bfs(i,j,high,low);
  54. //cout << i << " " << j << " " << high << " " << low << endl;
  55. if(!high) valley ++;
  56. if(!low) peak ++;
  57. }
  58. printf("%d %d\n",peak,valley);
  59. }
  60. int main()
  61. {
  62. solve();
  63. system("pause");
  64. return 0;
  65. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/800408
推荐阅读
相关标签
  

闽ICP备14008679号