forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloodFill.java
62 lines (46 loc) · 1.67 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
import java.util.Scanner;
// This algorightm is used to fill color in picture in particular boundaries
public class FloodFill {
// These columns(M) and rows(N)
static int M = 8;
static int N = 8;
static void floodFill(int screen[][],int x,int y,int newSec,int prev) {
if(x<0 || y<0 || x>=M || y>=N || screen[x][y] != prev) {
return;
}
screen[x][y] = newSec;
floodFill(screen,x-1,y,newSec,prev);
floodFill(screen,x+1,y,newSec,prev);
floodFill(screen,x, y+1,newSec,prev);
floodFill(screen,x,y-1,newSec,prev);
}
public static void main(String[] args) {
// You can also input this screen array but have to change value of M and N.
int screen[][] = {{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},
};
Scanner sc = new Scanner(System.in);
System.out.print("Enter the character you want to replace: ");
int prev = sc.nextInt();
System.out.print("Enter the index(i,j) of : ");
int x = sc.nextInt();
int y = sc.nextInt();
System.out.print("Enter the new number: ");
int newC = sc.nextInt();
floodFill(screen, x, y, newC,prev);
System.out.println("Updated screen after call to floodFill: ");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
System.out.print(screen[i][j] + " ");
System.out.println();
}
sc.close();
}
}