-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspread_color.c
96 lines (89 loc) · 2.22 KB
/
spread_color.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
#include <stdio.h>
#include<stdbool.h>
#define M 5
#define N 8
char colors[4] = {'R', 'G', 'B', 'X'}; // Red, Green, Blue, Empty
void spread(char*, int, int);
int main(void) {
char graph[M][N];
int row, col;
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++)
scanf("%c", &graph[i][j]);
getchar(); // Ignore '\n'
}
scanf("%d %d", &row, &col); // Starting position
spread(&graph[0][0], row, col);
// Print out the spreading result
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++)
printf("%c", graph[i][j]);
printf("\n");
}
return 0;
}
void spread(char* graph, int row, int col) {
// printf("%c",*(graph+col+N*row));
char color=*(graph+col+N*row);
char rgb[]={'R','G','B','A'};
int spec[]={0,1,2};
switch(*(graph+col+N*row)){
case 'R' :
spec[0]=3;
break;
case 'G' :
spec[1]=3;
break;
case 'B' :
spec[2]=3;
break;
}
int i=1;
//upward
while(row-i>=0){
if(*(graph+col+N*(row-i))==rgb[spec[0]])
break;
if(*(graph+col+N*(row-i))==rgb[spec[1]])
break;
if(*(graph+col+N*(row-i))==rgb[spec[2]])
break;
*(graph+col+N*(row-i))=color;
i++;
}
//dowanward
i=1;
while(row+i<M){
if(*(graph+col+N*(row+i))==rgb[spec[0]])
break;
if(*(graph+col+N*(row+i))==rgb[spec[1]])
break;
if(*(graph+col+N*(row+i))==rgb[spec[2]])
break;
*(graph+col+N*(row+i))=color;
i++;
}
//right
i=1;
while(col+i<N){
if(*(graph+col+i+N*(row))==rgb[spec[0]])
break;
if(*(graph+col+i+N*(row))==rgb[spec[1]])
break;
if(*(graph+col+i+N*(row))==rgb[spec[2]])
break;
*(graph+col+i+N*(row))=color;
i++;
}
//left
i=1;
while(col-i>=0){
if(*(graph+col-i+N*(row))==rgb[spec[0]])
break;
if(*(graph+col-i+N*(row))==rgb[spec[1]])
break;
if(*(graph+col-i+N*(row))==rgb[spec[2]])
break;
*(graph+col-i+N*(row))=color;
i++;
}
}