C语言 | Leetcode C语言接雨水II

server/2024/9/23 4:29:19/

题目:

题解

typedef struct{int row;int column;int height;
} Element;struct Pri_Queue;
typedef struct Pri_Queue *P_Pri_Queue;
typedef Element Datatype;struct Pri_Queue{int n;Datatype *pri_qu;
};/*优先队列插入*/
P_Pri_Queue add_pri_queue(P_Pri_Queue pq, Datatype x){int i;if(pq->n == 0){pq->pri_qu[0] = x; pq->n ++; return(pq);}else{for(i = pq->n; (i > 0) && (x.height < pq->pri_qu[(i - 1) / 2].height); i = (i - 1) / 2){pq->pri_qu[i] = pq->pri_qu[(i - 1) / 2];}}pq->pri_qu[i] = x;pq->n ++;return(pq);
}/*优先队列删除*/
P_Pri_Queue del_pri_queue(P_Pri_Queue pq){int i = 0;if(pq->n > 1){while(i <= (pq->n - 2) / 2){if((pq->pri_qu[pq->n - 1].height > pq->pri_qu[2 * i + 1].height) || \((2 * i + 2 <= pq->n - 1) && (pq->pri_qu[pq->n - 1].height > pq->pri_qu[2 * i + 2].height))){if(2 * i + 2 <= pq->n - 1){if(pq->pri_qu[2 * i + 1].height > pq->pri_qu[2 * i + 2].height){pq->pri_qu[i] = pq->pri_qu[2 * i + 2];i = 2 * i + 2;}else{pq->pri_qu[i] = pq->pri_qu[2 * i + 1];i = 2 * i + 1;}}else{pq->pri_qu[i] = pq->pri_qu[2 * i + 1];i = 2 * i + 1;}}else{pq->pri_qu[i] = pq->pri_qu[pq->n - 1];pq->n --;return(pq);}}}pq->pri_qu[i] = pq->pri_qu[pq->n - 1];pq->n --;return(pq);
}int trapRainWater(int** heightMap, int heightMapSize, int* heightMapColSize){if((heightMapSize < 3) || (*heightMapColSize < 3)) return(0);int Used_heightMap[112][112] = {0}, i, j, ans = 0;int direct[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};Datatype x;P_Pri_Queue pq;pq = (P_Pri_Queue)malloc(sizeof(struct Pri_Queue));pq->n = 0; pq->pri_qu = (Datatype*)malloc(sizeof(Datatype) * heightMapSize * (*heightMapColSize));for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][0] = 1;for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][1] = 1;for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][*heightMapColSize] = 1;for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][*heightMapColSize + 1] = 1;for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[0][j] = 1;for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[1][j] = 1;for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[heightMapSize][j] = 1;for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[heightMapSize + 1][j] = 1;for(i = 1; i < heightMapSize - 1; i ++){x.row = i; x.column = 0; x.height = heightMap[i][0];add_pri_queue(pq, x);}for(i = 1; i < heightMapSize - 1; i ++){x.row = i; x.column = *heightMapColSize - 1; x.height = heightMap[i][*heightMapColSize - 1];add_pri_queue(pq, x);}for(j = 1; j < *heightMapColSize - 1; j ++){x.row = 0; x.column = j; x.height = heightMap[0][j];add_pri_queue(pq, x);}for(j = 1; j < *heightMapColSize - 1; j ++){x.row = heightMapSize - 1; x.column = j; x.height = heightMap[heightMapSize - 1][j];add_pri_queue(pq, x);}while(pq->n > 0){for(i = 0; i < 4; i ++){if(Used_heightMap[pq->pri_qu[0].row + 1 + direct[i][0]][pq->pri_qu[0].column + 1 + direct[i][1]] == 0){if(heightMap[pq->pri_qu[0].row + direct[i][0]][pq->pri_qu[0].column + direct[i][1]] < pq->pri_qu[0].height){ans += pq->pri_qu[0].height - heightMap[pq->pri_qu[0].row + direct[i][0]][pq->pri_qu[0].column + direct[i][1]];x.height = pq->pri_qu[0].height;}else{x.height = heightMap[pq->pri_qu[0].row + direct[i][0]][pq->pri_qu[0].column + direct[i][1]];}x.row = pq->pri_qu[0].row + direct[i][0]; x.column = pq->pri_qu[0].column + direct[i][1];add_pri_queue(pq, x);Used_heightMap[pq->pri_qu[0].row + 1 + direct[i][0]][pq->pri_qu[0].column + 1 + direct[i][1]] = 1;}}//printf("row = %d, column =  %d, height = %d, n = %d\n", pq->pri_qu[0].row, pq->pri_qu[0].column, pq->pri_qu[0].height, pq->n);del_pri_queue(pq);//printf("row = %d, column =  %d, height = %d, n = %d\n", pq->pri_qu[0].row, pq->pri_qu[0].column, pq->pri_qu[0].height, pq->n);}return(ans);
}

http://www.ppmy.cn/server/120621.html

相关文章

网页与微信小程序:一场轻量化应用的博弈

网页与微信小程序&#xff1a;一场轻量化应用的博弈 在如今的信息时代&#xff0c;移动互联网已然成为主流&#xff0c;而在这一趋势的驱动下&#xff0c;应用形态也在不断演变。微信小程序与传统网页&#xff0c;作为两种不同的应用形态&#xff0c;正如两条并行却又交织的道…

Facebook直播限流是什么原因?是ip地址导致的吗

随着社交媒体和直播行业的蓬勃发展&#xff0c;Facebook直播已成为众多企业和个人进行品牌推广、产品展示和互动交流的重要平台。然而&#xff0c;在享受直播带来的便利与效益的同时&#xff0c;不少用户也面临着直播限流的困扰。本文将探讨Facebook直播限流的原因&#xff0c;…

2.个人电脑部署MySQL,傻瓜式教程带你拥有个人金融数据库!

2.个人电脑部署MySQL&#xff0c;傻瓜式教程带你拥有个人金融数据库&#xff01; ‍ 前边我们提到&#xff0c;比较适合做量化投研的数据库是MySQL&#xff0c;开源免费。所以今天我就写一篇教程来教大家如何在自己的环境中部署MySQL。 在不同的设备或系统中安装MySQL的步骤…

基于51单片机的汽车倒车防撞报警器系统

目录 一、主要功能 二、硬件资源 三、程序编程 四、实现现象 一、主要功能 本课题基于微控制器控制器&#xff0c; 设计一款汽车倒车防撞报警器系统。 要求&#xff1a; 要求&#xff1a;1.配有距离&#xff0c; 用于把车和障碍物之间的距离信号送入控制器。 2.配有报警系…

【Java】线程暂停比拼:wait() 和 sleep()的较量

欢迎浏览高耳机的博客 希望我们彼此都有更好的收获 感谢三连支持&#xff01; 在Java多线程编程中&#xff0c;合理地控制线程的执行是至关重要的。wait()和sleep()是两个常用的方法&#xff0c;它们都可以用来暂停线程的执行&#xff0c;但它们之间存在着显著的差异。本文将详…

ARM驱动学习之9注册字符类设备

ARM驱动学习之9注册字符类设备 • 分配内存空间函数kmalloc – 分配连续的虚拟地址&#xff0c;用于小内存分配。在include/linux/slab.h文件中。 – 参数1&#xff1a;申请的内存大小(最大128K)&#xff0c; – 参数2&#xff1a;GFP_KERNEL&#xff0c;代表优先权&#xff0…

09年408考研真题解析-计算机网络

[题34]在无噪声情况下&#xff0c;若某通信链路的带宽为3kHz&#xff0c;采用4个相位&#xff0c;每个相位具有4种振幅的QAM调制技术,则该通信链路的最大数据传输速率是&#xff08;B&#xff09; A.12 kbps B.24 kbps C.48 kbps D.96 kbps 解析&#xff…

C++ | Leetcode C++题解之第415题字符串相加

题目&#xff1a; 题解&#xff1a; class Solution { public:string addStrings(string num1, string num2) {int i num1.length() - 1, j num2.length() - 1, add 0;string ans "";while (i > 0 || j > 0 || add ! 0) {int x i > 0 ? num1[i] - 0 …