当前位置: 首页 > article >正文

C语言 | Leetcode C语言题解之第417题太平洋大西洋水流问题

题目:

题解:

static const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void bfs(int row, int col, bool ** ocean, const int ** heights, int m, int n) {
    if (ocean[row][col]) {
        return;
    }
    ocean[row][col] = true;
    int * queue = (int *)malloc(sizeof(int) * m * n);
    int head = 0;
    int tail = 0;
    queue[tail++] = row * n + col;
    while (head != tail) {
        int row = queue[head] / n;
        int col = queue[head] % n;
        head++;
        for (int i = 0; i < 4; i++) {
            int newRow = row + dirs[i][0], newCol = col + dirs[i][1];
            if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && heights[newRow][newCol] >= heights[row][col] && !ocean[newRow][newCol]) {
                ocean[newRow][newCol] = true;
                queue[tail++] = newRow * n + newCol;
            }
        }
    }
    free(queue);
}

int** pacificAtlantic(int** heights, int heightsSize, int* heightsColSize, int* returnSize, int** returnColumnSizes){
        int m = heightsSize;
        int n = heightsColSize[0];
        bool ** pacific = (bool **)malloc(sizeof(bool *) * m);
        bool ** atlantic = (bool **)malloc(sizeof(bool *) * m);
        for (int i = 0; i < m; i++) {
            pacific[i] = (bool *)malloc(sizeof(bool) * n);
            atlantic[i] = (bool *)malloc(sizeof(bool) * n);
            memset(pacific[i], 0, sizeof(bool) * n);
            memset(atlantic[i], 0, sizeof(bool) * n);
        }

        for (int i = 0; i < m; i++) {
            bfs(i, 0, pacific, heights, m, n);
        }
        for (int j = 1; j < n; j++) {
            bfs(0, j, pacific, heights, m, n);
        }
        for (int i = 0; i < m; i++) {
            bfs(i, n - 1, atlantic, heights, m, n);
        }
        for (int j = 0; j < n - 1; j++) {
            bfs(m - 1, j, atlantic, heights, m, n);
        }
        int ** result = (int **)malloc(sizeof(int *) * m * n);
        *returnColumnSizes = (int *)malloc(sizeof(int) * m * n);
        int pos = 0;
        for (int i = 0; i < m * n; i++) {
            result[i] = (int *)malloc(sizeof(int) * 2);
            (*returnColumnSizes)[i] = 2;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (pacific[i][j] && atlantic[i][j]) {
                    result[pos][0] = i;
                    result[pos][1] = j;
                    pos++;
                }
            }
            free(pacific[i]);
            free(atlantic[i]);
        }
        free(pacific);
        free(atlantic);
        *returnSize = pos;
        return result;
}

http://www.kler.cn/news/313603.html

相关文章:

  • ARM/Linux嵌入式面经(三八):绿盟科技
  • SpringBoot:自定义异常
  • string类,vector<T>,iterator迭代器,C风格字符串,数组
  • Apache James配置连接达梦数据库
  • Spring面试题合集
  • Nexus3的妙用
  • re题(27)BUUFCTF-[MRCTF2020]Transform
  • 【系统架构设计师】专题:软件架构风格(详细知识点及历年真题)
  • 使用 Go 语言实现简单聊天系统
  • 排序算法-归并排序
  • 深入解析 JVM 运行时数据区:实战与面试指南
  • Qt clicked()、clicked(bool)、toggled(bool)信号的区别和联系
  • C#基础(11)函数重载
  • 使用jenkins打包unity工程
  • LeetCode118:杨辉三角
  • Spring Boot- 配置文件问题
  • 【JavaScript】数据结构之链表(双指针、滑动窗口)
  • 切换淘宝最新镜像源npm详细讲解
  • 计算机毕业设计选题推荐-4S店试驾平台-小程序/App
  • 过采样和欠采样
  • C++ 字符串最后一个单词的长度(牛客网)
  • # wps必须要登录激活才能使用吗?
  • 摄影学习平台
  • 【Linux】简易日志系统
  • Web前端开发
  • PHP 数组排序类型介绍
  • 基于微信小程序的剧本杀游玩一体化平台
  • [数据结构]算法复杂度详解
  • 代码随想录算法训练营Day7
  • 基于MySQL全量备份+GTID同步的主从架构恢复数据至指定时间点