-
Notifications
You must be signed in to change notification settings - Fork 5
/
FloodFill.java
111 lines (95 loc) · 2.89 KB
/
FloodFill.java
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
/*
Given a 2D screen, location of a pixel in the screen ie(x,y) and a color(K), your task is to replace color of the given pixel and all adjacent(excluding diagonally adjacent) same colored pixels with the given color K.
Example:
{{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 1},
{1, 2, 2, 2, 2, 0, 1, 0},
{1, 1, 1, 2, 2, 0, 1, 0},
{1, 1, 1, 2, 2, 2, 2, 0},
{1, 1, 1, 1, 1, 2, 1, 1},
{1, 1, 1, 1, 1, 2, 2, 1},
};
x=4, y=4, color=3
{{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 1},
{1, 3, 3, 3, 3, 0, 1, 0},
{1, 1, 1, 3, 3, 0, 1, 0},
{1, 1, 1, 3, 3, 3, 3, 0},
{1, 1, 1, 1, 1, 3, 1, 1},
{1, 1, 1, 1, 1, 3, 3, 1}, };
Note: Use zero based indexing.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. The first line of each test case contains Two integers N and M denoting the size of the matrix. Then in the next line are N*M space separated values of the matrix. Then in the next line are three values x, y and K.
Output:
For each test case print the space separated values of the new matrix.
Constraints:
1<=T<=100
1<=M[][]<=100
Example:
Input:
3
3 4
0 1 1 0 1 1 1 1 0 1 2 3
0 1 5
2 2
1 1 1 1
0 1 8
4 4
1 2 3 4 1 2 3 4 1 2 3 4 1 3 2 4
0 2 9
Output:
0 5 5 0 5 5 5 5 0 5 2 3
8 8 8 8
1 2 9 4 1 2 9 4 1 2 9 4 1 3 2 4
*/
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int m = sc.nextInt();
int n = sc.nextInt();
int[][] arr =new int[m][n];
for(int i = 0; i<m; i++)
{
for(int j = 0; j<n;j++)
{
arr[i][j] = sc.nextInt();
}
}
int x = sc.nextInt();
int y = sc.nextInt();
int up = sc.nextInt();
int org = arr[x][y];
upgradeColor(arr, x, y , up, org);
for(int i = 0; i<m; i++)
{
for(int j = 0; j<n;j++)
{
System.out.print(arr[i][j] +" ");
}
}
System.out.println();
}}
public static void upgradeColor(int[][] arr, int x, int y, int c, int org)
{
arr[x][y] = c;
if((x+1)<arr.length && arr[x+1][y] == org )
{
upgradeColor(arr,x+1,y,c,org);
}
if((x-1) > 0 && arr[x-1][y] == org)
upgradeColor(arr,x-1,y,c,org);
if((y+1)<arr[0].length && arr[x][y+1] == org)
upgradeColor(arr,x,y+1,c,org);
if((y-1) > 0 && arr[x][y-1] == org)
upgradeColor(arr,x,y-1,c,org);
}
}