UVa 815 Flooded!

UVa 815 Flooded!
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

Flooded!

Time Limit: Unknown Memory Limit: Unknown
Total Submission(s): Unknown Accepted Submission(s): Unknown


https://uva.onlinejudge.org/i…


Accepted Code

// Author : Weihao Long
// Created : 2017/12/31
#include "stdio.h"
#define MAX 1000
struct node {
int level;     // 海拔
int space;     // 底面積
int flag;     // 土地標誌
};
int main() {
int m, n;
int region = 0;
while (scanf("%d%d", &m, &n) != EOF) {
if (m == 0 && n == 0)
break;
region  = 1;
// 輸入土地海拔和雨水量
node a[MAX];
int t = m * n;
for (int i = 0; i < t; i  ) {
scanf("%d", &a[i].level);
a[i].space = 100;
a[i].flag = 1;
}
int rain;
scanf("%d", &rain);
// 按海拔升序
for (int i = 1; i < t; i  ) {
for (int k = 0; k < t - i; k  ) {
if (a[k].level > a[k   1].level) {
node tmp = a[k];
a[k] = a[k   1];
a[k   1] = tmp;
}
}
}
// 把海拔相同的土地歸併
int i, k;
for (i = 0; i < t - 1; i = k) {
for (k = i   1; a[i].level == a[k].level; k  ) {
a[i].space  = a[k].space;
a[k].flag = 0;
a[i].flag  = 1;
}
}
// 把有效土地存到另一個陣列
node b[MAX];
k = 0;
for (i = 0; i < t; i  ) {
if (a[i].flag) {
b[k  ] = a[i];
}
}
b[k].level = 0xFFFFFFF;     // 在末尾加個無限高的牆
t = k   1;
// 模擬注水過程
int cap = 0;
double level = 0;
for (i = 0; i < t - 1; i  ) {
cap = b[i].space * (b[i   1].level - b[i].level);
b[i   1].space  = b[i].space;
rain -= cap;
level = b[i   1].level;
if (rain <= 0) {
rain  = cap;
level = b[i].level;
level  = 1.0 * rain / b[i].space;
break;
}
}
int count = 0;
for (k = 0; k <= i; k  ) {
count  = b[k].flag;
}
double area = 100.0 * count / (m * n);
// 輸出結果
printf("Region %d\n", region);
printf("Water level is %.2lf meters.\n", level);
printf("%.2lf percent of the region is under water.\n\n", area);
}
return 0;
}

Notes

題意:
有一個 m*n 的網格,每個格子是邊長為 10 米的正方形,網格四周是無限高的牆。輸入每個格子的海拔高度,以及網格內雨水的總體積,輸出水位的海拔高度以及有多少百分比的區域內有水(即高度嚴格小於水平面)。

思路:
將土地依照海拔升序排成一列,模擬注水過程即可。

相關文章

程式語言 最新文章