-
Notifications
You must be signed in to change notification settings - Fork 5
/
2024_06_08-Lesson.c
123 lines (109 loc) · 2.11 KB
/
2024_06_08-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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#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');
}
}
/* look in the current column `c` for a star */
int scan_column(char *g,int top,int left)
{
int scandown;
for( scandown=top+1; scandown<ROWS; scandown++ )
{
if( *(g+scandown*COLS+left) == '*' )
return(scandown);
}
return(0);
}
int find_right(char *g,int top,int left,int bottom)
{
int right;
for( right=left+1; right<COLS; right++ )
{
if( *(g+top*COLS+right)=='*' && *(g+bottom*COLS+right)=='*' )
return right;
}
return(0);
}
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 */
r.bottom = scan_column(grid,r.top,r.left);
if(r.bottom)
{
r.right = find_right(grid,r.top,r.left,r.bottom);
if(r.right)
{
count++;
printf("Rectangle %d:\n",count);
output_grid(grid,r);
}
}
}
}
}
printf("Found %d rectangles\n",count);
return 0;
}