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


    题目:

    题解:

    1. typedef struct{
    2. int row;
    3. int column;
    4. int height;
    5. } Element;
    6. struct Pri_Queue;
    7. typedef struct Pri_Queue *P_Pri_Queue;
    8. typedef Element Datatype;
    9. struct Pri_Queue{
    10. int n;
    11. Datatype *pri_qu;
    12. };
    13. /*优先队列插入*/
    14. P_Pri_Queue add_pri_queue(P_Pri_Queue pq, Datatype x){
    15. int i;
    16. if(pq->n == 0){
    17. pq->pri_qu[0] = x; pq->n ++; return(pq);
    18. }
    19. else{
    20. for(i = pq->n; (i > 0) && (x.height < pq->pri_qu[(i - 1) / 2].height); i = (i - 1) / 2){
    21. pq->pri_qu[i] = pq->pri_qu[(i - 1) / 2];
    22. }
    23. }
    24. pq->pri_qu[i] = x;
    25. pq->n ++;
    26. return(pq);
    27. }
    28. /*优先队列删除*/
    29. P_Pri_Queue del_pri_queue(P_Pri_Queue pq){
    30. int i = 0;
    31. if(pq->n > 1){
    32. while(i <= (pq->n - 2) / 2){
    33. if((pq->pri_qu[pq->n - 1].height > pq->pri_qu[2 * i + 1].height) || \
    34. ((2 * i + 2 <= pq->n - 1) && (pq->pri_qu[pq->n - 1].height > pq->pri_qu[2 * i + 2].height))){
    35. if(2 * i + 2 <= pq->n - 1){
    36. if(pq->pri_qu[2 * i + 1].height > pq->pri_qu[2 * i + 2].height){
    37. pq->pri_qu[i] = pq->pri_qu[2 * i + 2];
    38. i = 2 * i + 2;
    39. }
    40. else{
    41. pq->pri_qu[i] = pq->pri_qu[2 * i + 1];
    42. i = 2 * i + 1;
    43. }
    44. }
    45. else{
    46. pq->pri_qu[i] = pq->pri_qu[2 * i + 1];
    47. i = 2 * i + 1;
    48. }
    49. }
    50. else{
    51. pq->pri_qu[i] = pq->pri_qu[pq->n - 1];
    52. pq->n --;
    53. return(pq);
    54. }
    55. }
    56. }
    57. pq->pri_qu[i] = pq->pri_qu[pq->n - 1];
    58. pq->n --;
    59. return(pq);
    60. }
    61. int trapRainWater(int** heightMap, int heightMapSize, int* heightMapColSize){
    62. if((heightMapSize < 3) || (*heightMapColSize < 3)) return(0);
    63. int Used_heightMap[112][112] = {0}, i, j, ans = 0;
    64. int direct[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    65. Datatype x;
    66. P_Pri_Queue pq;
    67. pq = (P_Pri_Queue)malloc(sizeof(struct Pri_Queue));
    68. pq->n = 0; pq->pri_qu = (Datatype*)malloc(sizeof(Datatype) * heightMapSize * (*heightMapColSize));
    69. for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][0] = 1;
    70. for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][1] = 1;
    71. for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][*heightMapColSize] = 1;
    72. for(i = 0; i < heightMapSize + 2; i ++) Used_heightMap[i][*heightMapColSize + 1] = 1;
    73. for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[0][j] = 1;
    74. for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[1][j] = 1;
    75. for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[heightMapSize][j] = 1;
    76. for(j = 0; j < *heightMapColSize + 2; j ++) Used_heightMap[heightMapSize + 1][j] = 1;
    77. for(i = 1; i < heightMapSize - 1; i ++){
    78. x.row = i; x.column = 0; x.height = heightMap[i][0];
    79. add_pri_queue(pq, x);
    80. }
    81. for(i = 1; i < heightMapSize - 1; i ++){
    82. x.row = i; x.column = *heightMapColSize - 1; x.height = heightMap[i][*heightMapColSize - 1];
    83. add_pri_queue(pq, x);
    84. }
    85. for(j = 1; j < *heightMapColSize - 1; j ++){
    86. x.row = 0; x.column = j; x.height = heightMap[0][j];
    87. add_pri_queue(pq, x);
    88. }
    89. for(j = 1; j < *heightMapColSize - 1; j ++){
    90. x.row = heightMapSize - 1; x.column = j; x.height = heightMap[heightMapSize - 1][j];
    91. add_pri_queue(pq, x);
    92. }
    93. while(pq->n > 0){
    94. for(i = 0; i < 4; i ++){
    95. if(Used_heightMap[pq->pri_qu[0].row + 1 + direct[i][0]][pq->pri_qu[0].column + 1 + direct[i][1]] == 0){
    96. if(heightMap[pq->pri_qu[0].row + direct[i][0]][pq->pri_qu[0].column + direct[i][1]] < pq->pri_qu[0].height){
    97. ans += pq->pri_qu[0].height - heightMap[pq->pri_qu[0].row + direct[i][0]][pq->pri_qu[0].column + direct[i][1]];
    98. x.height = pq->pri_qu[0].height;
    99. }
    100. else{x.height = heightMap[pq->pri_qu[0].row + direct[i][0]][pq->pri_qu[0].column + direct[i][1]];}
    101. x.row = pq->pri_qu[0].row + direct[i][0]; x.column = pq->pri_qu[0].column + direct[i][1];
    102. add_pri_queue(pq, x);
    103. Used_heightMap[pq->pri_qu[0].row + 1 + direct[i][0]][pq->pri_qu[0].column + 1 + direct[i][1]] = 1;
    104. }
    105. }
    106. //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);
    107. del_pri_queue(pq);
    108. //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);
    109. }
    110. return(ans);
    111. }
  • 相关阅读:
    图像处理算法实战【2】读取图片并获取指定像素位置的RGB值
    C/C++编程工具及实用小软件推荐
    ubuntu16.04+cuda10.0+cudnn7.6+tensorflow_gpu-1.11.0环境安装
    短期动态IP介绍
    Kimi多线程批量写原创文章API软件
    elasticsearch-dump 迁移es数据 (elasticdump)
    VC++检测和解决GDI泄漏问题
    内核学习——1、list_head
    kubernetes重新生成证书,重新生成配置文件
    手写RPC框架-注册中心实现
  • 原文地址:https://blog.csdn.net/m0_59237910/article/details/142292302