-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion23.c
75 lines (70 loc) · 2.08 KB
/
question23.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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int isValid(int rows, int columns, int i, int j){
if(i < rows && i >= 0 && j < columns && j>= 0){
return 1;
}
return 0;
}
void updateColors(int rows, int columns, int arr[rows][columns], int i, int j, int color, int initial){
// printf("initial is %d\n",initial);
int k,l;
if(isValid(rows,columns,i,j) && arr[i][j] == initial){
arr[i][j] = color;
}
// printf("entering the for loop.......................................\n");
for(k = i-1; k <= i+1; k++){
for(l = j-1; l <= j+1; l++){
if(l == j && k == i){
continue;
}
if((k == i-1 && l == j-1) || (k == i-1 && l == j+1) || (k == i+1 && l == j-1) || (k == i+1 && l == j+1)){
continue;
}
// printf("value of arr[%d][%d] is %d\n",k,l,arr[k][l]);
// printf("value of initial is %d\n",initial);
if(isValid(rows,columns,k,l) && arr[k][l] == initial){
// printf("upating........\n");
arr[k][l] = color;
// printf("entering the for loop.......................................\n");
updateColors(rows,columns, arr,k,l,color,initial);
}
}
}
// printf("printing the array here...............................\n");
// int p,q;
// for(p=0;p<rows;p++){
// for(q=0;q<columns;q++){
// printf("%d ",arr[p][q]);
// }
// }
// printf("\n");
}
int main(){
int cases;
scanf("%d",&cases);
int i;
for(i=0;i<cases;i++){
int n,m;
scanf("%d %d",&n, &m);
int p,q;
int arr[n][m];
for(p=0;p<n;p++){
for(q=0;q<m;q++){
scanf("%d",&arr[p][q]);
}
}
int x,y,k;
scanf("%d %d %d",&x,&y,&k);
updateColors(n,m,arr,x,y,k, arr[x][y]);
for(p=0;p<n;p++){
for(q=0;q<m;q++){
printf("%d ",arr[p][q]);
}
}
printf("\n");
}
return 0;
}