java flood fill
Java
// Java program to implement flood fill algorithm
class GFG
{
// Dimentions of paint screen
static int M = 8;
static int N = 8;
// A recursive function to replace previous color 'prevC' at '(x, y)'
// and all surrounding pixels of (x, y) with new color 'newC' and
static void floodFillUtil(int screen[][], int x, int y,
int prevC, int newC)
{
// Base cases
if (x < 0 || x >= M || y < 0 || y >= N)
return;
if (screen[x][y] != prevC)
return;
// Replace the color at (x, y)
screen[x][y] = newC;
// Recur for north, east, south and west
floodFillUtil(screen, x+1, y, prevC, newC);
floodFillUtil(screen, x-1, y, prevC, newC);
floodFillUtil(screen, x, y+1, prevC, newC);
floodFillUtil(screen, x, y-1, prevC, newC);
}
// It mainly finds the previous color on (x, y) and
// calls floodFillUtil()
static void floodFill(int screen[][], int x, int y, int newC)
{
int prevC = screen[x][y];
floodFillUtil(screen, x, y, prevC, newC);
}
public static void main(String[] args)
{
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},
};
int x = 4, y = 4, newC = 3;
floodFill(screen, x, y, newC);
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();
}
}
}
Also in Java:
- Title
- java code to save excel data to mysql
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- java how to initialize an array
- Category
- Java
- Title
- how to change all the text in a textArea java at once
- Category
- Java
- Title
- convert array of int to arraylist java
- Category
- Java
- Title
- priority queue in java
- Category
- Java
- Title
- how to play a clip of audio in java when needed
- Category
- Java
- Title
- java add a list to a list
- Category
- Java
- Title
- java replace all html tags
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- java loop
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- java empty array
- Category
- Java
- Title
- méthode retourne nom classe java
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- loop while in java
- Category
- Java
- Title
- how to create an array list in java
- Category
- Java
- Title
- java pass array as method parameter
- Category
- Java
- Title
- get tfidf score for a sentence
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- binary numbers java
- Category
- Java
- Title
- scan a string using scanner class in java
- Category
- Java
- Title
- validation list empty java
- Category
- Java
- Title
- java script how to not allow soace
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- How to make a class in Java?
- Category
- Java
- Title
- java read file from command line argument
- Category
- Java
- Title
- set top corner of shape radius programmatically android
- Category
- Java
- Title
- throwing exceptions java
- Category
- Java
- Title
- store string elements in character array in java
- Category
- Java
- Title
- java script find screen size of device
- Category
- Java
- Title
- log10 in java
- Category
- Java
- Title
- crit chance in java
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- substring in java
- Category
- Java
- Title
- maximum arrays size in java
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- spigot chat color with symbol
- Category
- Java
- Title
- summary of operators java
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- map.keyset in java
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- initialize arraylist
- Category
- Java
- Title
- How to create a 2d array in java
- Category
- Java
- Title
- java list as parameter
- Category
- Java
- Title
- isnumber java
- Category
- Java
- Title
- how to make stage dragable in javafx
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- how to change actionbar color in android programmatically
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- android studio remove notification bar
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- Manifest merger failed androidx
- Category
- Java
- Title
- android how to switch between activities
- Category
- Java
- Title
- java function without return
- Category
- Java
- Title
- stackoverflow java enum with constructor
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- java Convert a string IPv4 IP address to the equivalent long numeric value.
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- absolute value in java
- Category
- Java
- Title
- java insert into arraylist
- Category
- Java
- Title
- Java copy file
- Category
- Java
- Title
- system.in example in java
- Category
- Java
- Title
- for next loop javasxcrop
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- deserialize date java
- Category
- Java
- Title
- java compareto
- Category
- Java
- Title
- how to get the checkbox checked value in javafx
- Category
- Java
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- java convert edittext to double
- Category
- Java
- Title
- java syntax
- Category
- Java
- Title
- java class array of objects
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- spring org.springframework.stereotype not visible
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet
- Category
- Java
- Title
- code to include layout from java in android
- Category
- Java
- Title
- set html text android java
- Category
- Java
- Title
- the type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from required .class files
- Category
- Java
- Title
- how to add strings together
- Category
- Java
- Title
- java how to show an array
- Category
- Java