• 洛谷 1596.Lake Counting 5


    英文题目最好自己翻译一下做,不要总是依赖中文翻译

    本道题和细胞数目一样,是一道典型的连通DFS问题。

    思路:我们看准,这里说的是W是水,.是旱地。所以我们只遍历W连通的地方。

    双层循环遍历这个地图,之后对于每一个枚举的点进行判断:

    1.是不是水‘W’?

    2.这里的点是不是被遍历过了?

    然后我们就对于每一个点进行DFS遍历。对于DFS的函数来说,我们只需要判断这个点周围的点是不是水就行了,也就是周围这八个点,有个时候是4个点,我们需要灵活应变,这里的zoux和zouy就是对于方向的变化数组。

    上代码:

    1. #include<iostream>
    2. #include<stdio.h>
    3. #include<cstring>
    4. #include<cstdlib>
    5. #include<cmath>
    6. #include<vector>
    7. #include<algorithm>
    8. #include<stack>
    9. #include<queue>
    10. #include<sstream>
    11. #include<map>
    12. #include<limits.h>
    13. #include<set>
    14. #define MAX 105
    15. #define _for(i,a,b) for(int i=a;i<(b);i++)
    16. #define ALL(x) x.begin(),x.end()
    17. using namespace std;
    18. typedef long long LL;
    19. int n, m, counts=0;
    20. LL A, B;
    21. int res = 0;
    22. int st[MAX][MAX];
    23. bool flag = false;
    24. char maps[MAX][MAX];
    25. int zoux[] = { -1,0,1,0,-1,-1,1,1};
    26. int zouy[] = { 0,1,0,-1,1,-1,-1,1};
    27. void dfs(int x, int y) {
    28. _for(i, 0, 8) {
    29. int a = x + zoux[i];
    30. int b = y + zouy[i];
    31. if (st[a][b])
    32. continue;
    33. if (a >= n || a < 0 || b >= m || b < 0)
    34. continue;
    35. if (maps[a][b] != 'W')
    36. continue;
    37. st[a][b] = 1;
    38. dfs(a, b);
    39. }
    40. }
    41. int main() {
    42. ios::sync_with_stdio(false);
    43. cin.tie(NULL); cout.tie(NULL);
    44. cin >> n >> m;
    45. _for(i, 0, n) {
    46. _for(j, 0, m) {
    47. cin >> maps[i][j];
    48. }
    49. }
    50. _for(i, 0, n) {
    51. _for(j, 0, m) {
    52. if (!st[i][j]&&maps[i][j]=='W') {
    53. st[i][j] = 1;
    54. dfs(i, j);
    55. res++;
    56. }
    57. }
    58. }
    59. cout << res << endl;
    60. return 0;
    61. }

  • 相关阅读:
    请教一个私人问题私人问题
    C/C++---------------LeetCode第1394.找出数组中的幸运数
    深度学习 Transformer架构解析
    十天学前端之JS篇(三)
    python中的小tips
    16.2.2 创建存储函数
    公众号迁移操作流程是怎样的?
    ccc-sklearn-10
    创建谷歌账号 绕过手机验证(2023.11亲测有效)
    MySQL InnoDB存储引擎的缓冲池和内存性能
  • 原文地址:https://blog.csdn.net/m0_73917165/article/details/136434289