-
Notifications
You must be signed in to change notification settings - Fork 5
/
2024_06_15-Lesson.c
102 lines (92 loc) · 1.81 KB
/
2024_06_15-Lesson.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 16
#define COLS ROWS*2
#define SIZE COLS*ROWS
#define PROB 5
struct rect {
int top;
int bottom;
int left;
int right;
};
void output_grid(char *g,struct rect r)
{
int row,col;
for( row=0; row<ROWS; row++ )
{
for( col=0; col<COLS; col++ )
{
if(
(row==r.top && (col>r.left && col<r.right) ) ||
(row==r.bottom && (col>r.left && col<r.right) )
)
putchar('-');
else if(
(col==r.left && (row>r.top && row<r.bottom) ) ||
(col==r.right && (row>r.top && row<r.bottom) )
)
putchar('|');
else
putchar( *(g+row*COLS+col) );
}
putchar('\n');
}
}
int main()
{
char *grid;
int row,col,count;
struct rect r;
/* seed the randomizer */
srand( (unsigned)time(NULL) );
/* allocate grid */
grid = malloc( sizeof(char) * SIZE );
if( grid==NULL )
{
fprintf(stderr,"Unable to allocate memory\n");
exit(1);
}
/* fill the grid */
for( row=0; row<ROWS; row++ )
{
for( col=0; col<COLS; col++ )
{
if( rand() % PROB )
*(grid+row*COLS+col) = '.';
else
*(grid+row*COLS+col) = '*';
}
}
count = 0;
/* scan for two in a column */
for( r.top=0; r.top<ROWS-1; r.top++ )
{
for( r.left=0; r.left<COLS; r.left++ )
{
/* find a star in the row */
if( *(grid+r.top*COLS+r.left) == '*' )
{
/* look for a matching star in the same column */
for( r.bottom=r.top+1; r.bottom<ROWS; r.bottom++ )
{
if( *(grid+r.bottom*COLS+r.left) == '*' )
{
for( r.right=r.left+1; r.right<COLS; r.right++ )
{
if( *(grid+r.top*COLS+r.right)=='*' && *(grid+r.bottom*COLS+r.right)=='*' )
{
count++;
printf("Rectangle %d:\n",count);
output_grid(grid,r);
}
}
}
}
}
}
}
printf("Found %d rectangles\n",count);
return 0;
}