# Programming randomly generates a lake similar to the following figure, in which the lake surface is represented by a closed polygon

2022-01-26 23:38:16

Program to randomly generate a lake similar to the following figure , The lake surface is represented by a closed polygon , The numeric value is greater than 0, Represents water depth , The digital value of shoreline is 0, The area of the lake is irregular , And the water depth is different everywhere in the lake . Calculate the area and average water depth of the lake , Output the generated lake surface graphics and calculation results .

xdm Help

For reference only ：

``// A large piece on the ground is long M rice , wide N Mi Gong M×N An uneven area composed of small pieces , The ground height is 0, The inside of each small piece is flat , Its height is 0～9 Rice doesn't wait .// After a heavy rain , How many cubic meters of accumulated water in this area ？// Suppose the water only flows due east 、 due south 、 Due west 、 In a small area adjacent to due north and lower in height ;// Instead of flowing to the Northeast 、 Southeast 、 The northwest 、 In a small area adjacent to the southwest and low in height .#include <stdio.h>#include <time.h>#include <stdlib.h>#define M 10#define N 10int a[M+4][N+4];// Ground height int w[M+4][N+4];// Water level int sp=0;int xs[(M+4)*(N+4)];int ys[(M+4)*(N+4)];int xx,yy;int x,y;int i,h;int tw;void push(int py,int px) {//  printf("%03d %d,%d\n",sp,py,px);    ys[sp]=py;    xs[sp]=px;    if (sp<(M+4)*(N+4)-1) {        sp=sp+1;    } else {        printf("stack overflow!\n");        exit(1);    }}void pop(void) {    sp=sp-1;    yy=ys[sp];    xx=xs[sp];}int check(int cy,int cx,int ch) {    if (a[cy][cx]<=ch && w[cy][cx]>ch) return 1;    else return 0;}void floodfilling(int hh) {    push(1,1);    while (1) {        if (sp<=0) break;//        pop();        if (check(yy,xx,hh)) {            w[yy][xx]=hh;            if (check(yy-1,xx  ,hh)) push(yy-1,xx  );            if (check(yy  ,xx+1,hh)) push(yy  ,xx+1);            if (check(yy  ,xx-1,hh)) push(yy  ,xx-1);            if (check(yy+1,xx  ,hh)) push(yy+1,xx  );        }    }    for (y=0;y<=M+3;y++) {        for (x=0;x<=N+3;x++) {            printf("%d%d ",a[y][x],w[y][x]);        }        printf("\n");    }    for (x=0;x<=N+3;x++) printf("---");    printf("hh=%d\n",hh);}void main() {    // Increase the height of the outermost ring by one circle 9 Small pieces     for (i=0;i<=M+3;i++) {a[i][0]=9;a[i][N+3]=9;}    for (i=0;i<=N+3;i++) {a[0][i]=9;a[M+3][i]=9;}    // Increase the height of the secondary outer ring by one circle 0 Small pieces     for (i=1;i<=M+2;i++) {a[i][1]=0;a[i][N+2]=0;}    for (i=1;i<=N+2;i++) {a[1][i]=0;a[M+2][i]=0;}    srand(time(NULL));    for (y=2;y<=M+1;y++) {        for (x=2;x<=N+1;x++) {            a[y][x]=rand()%10;// Initial ground height         }    }    for (y=0;y<=M+3;y++) {        for (x=0;x<=N+3;x++) {            w[y][x]=9;// Initial water surface height 9        }    }    printf(" At present, the ground height and water surface height of each small block \n",tw);    for (y=0;y<=M+3;y++) {        for (x=0;x<=N+3;x++) {            printf("%d%d ",a[y][x],w[y][x]);        }        printf("\n");    }    for (x=0;x<=N+3;x++) printf("---");    printf("hh=9\n");    for (h=8;h>=0;h--) floodfilling(h);// Check the water level 8 To 0    tw=0;    for (y=2;y<=M+1;y++) {        for (x=2;x<=N+1;x++) {            tw+=(w[y][x]-a[y][x]>0)?w[y][x]-a[y][x]:0;        }    }    printf(" Common ponding %d Cubic meters \n",tw);}// At present, the ground height and water surface height of each small block //99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 09 09 09 09 09 09 09 09 09 09 09 09 99//99 09 89 49 39 79 79 69 49 79 09 69 09 99//99 09 19 29 59 39 39 99 09 59 49 79 09 99//99 09 09 39 59 79 99 29 39 09 69 39 09 99//99 09 79 59 89 59 09 39 09 49 79 49 09 99//99 09 09 69 19 99 39 39 19 59 99 29 09 99//99 09 29 19 39 99 79 29 39 89 79 99 09 99//99 09 29 29 39 29 89 69 19 89 39 99 09 99//99 09 99 99 89 79 49 79 89 69 39 09 09 99//99 09 29 19 29 69 39 89 39 39 99 19 09 99//99 09 79 39 19 39 19 29 19 89 69 79 09 99//99 09 09 09 09 09 09 09 09 09 09 09 09 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=9//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 08 08 08 08 08 08 08 08 08 08 08 08 99//99 08 88 48 38 78 78 68 48 78 08 68 08 99//99 08 18 28 58 38 38 99 08 58 48 78 08 99//99 08 08 38 58 78 99 28 38 08 68 38 08 99//99 08 78 58 88 58 08 38 08 48 78 48 08 99//99 08 08 68 18 99 38 38 18 58 99 28 08 99//99 08 28 18 38 99 78 28 38 88 78 99 08 99//99 08 28 28 38 28 88 68 18 88 38 99 08 99//99 08 99 99 88 78 48 78 88 68 38 08 08 99//99 08 28 18 28 68 38 88 38 38 99 18 08 99//99 08 78 38 18 38 18 28 18 88 68 78 08 99//99 08 08 08 08 08 08 08 08 08 08 08 08 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=8//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 07 07 07 07 07 07 07 07 07 07 07 07 99//99 07 88 47 37 77 77 67 47 77 07 67 07 99//99 07 17 27 57 37 37 99 07 57 47 77 07 99//99 07 07 37 57 77 99 27 37 07 67 37 07 99//99 07 77 57 88 57 07 37 07 47 77 47 07 99//99 07 07 67 17 99 37 37 17 57 99 27 07 99//99 07 27 17 37 99 77 27 37 88 77 99 07 99//99 07 27 27 37 27 88 67 17 88 37 99 07 99//99 07 99 99 88 77 47 77 88 67 37 07 07 99//99 07 27 17 27 67 37 88 37 37 99 17 07 99//99 07 77 37 17 37 17 27 17 88 67 77 07 99//99 07 07 07 07 07 07 07 07 07 07 07 07 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=7//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 06 06 06 06 06 06 06 06 06 06 06 06 99//99 06 88 46 36 77 77 66 46 77 06 66 06 99//99 06 16 26 56 36 36 99 06 56 46 77 06 99//99 06 06 36 56 77 99 26 36 06 66 36 06 99//99 06 77 56 88 56 06 36 06 46 77 46 06 99//99 06 06 66 16 99 36 36 16 56 99 26 06 99//99 06 26 16 36 99 77 26 36 88 77 99 06 99//99 06 26 26 36 26 88 66 16 88 36 99 06 99//99 06 99 99 88 77 46 77 88 66 36 06 06 99//99 06 26 16 26 66 36 88 36 36 99 16 06 99//99 06 77 36 16 36 16 26 16 88 66 77 06 99//99 06 06 06 06 06 06 06 06 06 06 06 06 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=6//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 05 05 05 05 05 05 05 05 05 05 05 05 99//99 05 88 45 35 77 77 66 45 77 05 66 05 99//99 05 15 25 55 35 35 99 05 55 45 77 05 99//99 05 05 35 55 77 99 25 35 05 66 35 05 99//99 05 77 55 88 55 05 35 05 45 77 45 05 99//99 05 05 66 15 99 35 35 15 55 99 25 05 99//99 05 25 15 35 99 77 25 35 88 77 99 05 99//99 05 25 25 35 25 88 66 15 88 35 99 05 99//99 05 99 99 88 77 45 77 88 66 35 05 05 99//99 05 25 15 25 66 35 88 35 35 99 15 05 99//99 05 77 35 15 35 15 25 15 88 66 77 05 99//99 05 05 05 05 05 05 05 05 05 05 05 05 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=5//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 04 04 04 04 04 04 04 04 04 04 04 04 99//99 04 88 44 34 77 77 66 44 77 04 66 04 99//99 04 14 24 55 35 35 99 04 55 44 77 04 99//99 04 04 34 55 77 99 24 34 04 66 34 04 99//99 04 77 55 88 55 04 34 04 44 77 44 04 99//99 04 04 66 14 99 34 34 14 55 99 24 04 99//99 04 24 14 34 99 77 24 34 88 77 99 04 99//99 04 24 24 34 24 88 66 14 88 34 99 04 99//99 04 99 99 88 77 44 77 88 66 34 04 04 99//99 04 24 14 24 66 34 88 34 34 99 14 04 99//99 04 77 34 14 34 14 24 14 88 66 77 04 99//99 04 04 04 04 04 04 04 04 04 04 04 04 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=4//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 03 03 03 03 03 03 03 03 03 03 03 03 99//99 03 88 44 33 77 77 66 44 77 03 66 03 99//99 03 13 23 55 35 35 99 04 55 44 77 03 99//99 03 03 33 55 77 99 24 34 04 66 33 03 99//99 03 77 55 88 55 04 34 04 44 77 44 03 99//99 03 03 66 13 99 34 34 14 55 99 23 03 99//99 03 23 13 33 99 77 24 34 88 77 99 03 99//99 03 23 23 33 23 88 66 14 88 33 99 03 99//99 03 99 99 88 77 44 77 88 66 33 03 03 99//99 03 23 13 23 66 33 88 33 33 99 13 03 99//99 03 77 33 13 33 13 23 13 88 66 77 03 99//99 03 03 03 03 03 03 03 03 03 03 03 03 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=3//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 02 02 02 02 02 02 02 02 02 02 02 02 99//99 02 88 44 33 77 77 66 44 77 02 66 02 99//99 02 12 22 55 35 35 99 04 55 44 77 02 99//99 02 02 33 55 77 99 24 34 04 66 33 02 99//99 02 77 55 88 55 04 34 04 44 77 44 02 99//99 02 02 66 13 99 34 34 14 55 99 22 02 99//99 02 22 12 33 99 77 24 34 88 77 99 02 99//99 02 22 22 33 23 88 66 14 88 33 99 02 99//99 02 99 99 88 77 44 77 88 66 33 02 02 99//99 02 22 12 22 66 33 88 33 33 99 12 02 99//99 02 77 33 12 33 12 22 12 88 66 77 02 99//99 02 02 02 02 02 02 02 02 02 02 02 02 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=2//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 01 01 01 01 01 01 01 01 01 01 01 01 99//99 01 88 44 33 77 77 66 44 77 01 66 01 99//99 01 11 22 55 35 35 99 04 55 44 77 01 99//99 01 01 33 55 77 99 24 34 04 66 33 01 99//99 01 77 55 88 55 04 34 04 44 77 44 01 99//99 01 01 66 13 99 34 34 14 55 99 22 01 99//99 01 22 12 33 99 77 24 34 88 77 99 01 99//99 01 22 22 33 23 88 66 14 88 33 99 01 99//99 01 99 99 88 77 44 77 88 66 33 01 01 99//99 01 22 12 22 66 33 88 33 33 99 11 01 99//99 01 77 33 11 33 11 22 11 88 66 77 01 99//99 01 01 01 01 01 01 01 01 01 01 01 01 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=1//99 99 99 99 99 99 99 99 99 99 99 99 99 99//99 00 00 00 00 00 00 00 00 00 00 00 00 99//99 00 88 44 33 77 77 66 44 77 00 66 00 99//99 00 11 22 55 35 35 99 04 55 44 77 00 99//99 00 00 33 55 77 99 24 34 04 66 33 00 99//99 00 77 55 88 55 04 34 04 44 77 44 00 99//99 00 00 66 13 99 34 34 14 55 99 22 00 99//99 00 22 12 33 99 77 24 34 88 77 99 00 99//99 00 22 22 33 23 88 66 14 88 33 99 00 99//99 00 99 99 88 77 44 77 88 66 33 00 00 99//99 00 22 12 22 66 33 88 33 33 99 11 00 99//99 00 77 33 11 33 11 22 11 88 66 77 00 99//99 00 00 00 00 00 00 00 00 00 00 00 00 99//99 99 99 99 99 99 99 99 99 99 99 99 99 99//------------------------------------------hh=0// Common ponding 40 Cubic meters ``