Skip to content

Instantly share code, notes, and snippets.

@rafirh
Created November 7, 2023 05:08
Show Gist options
  • Save rafirh/9e43bd4ff7f1e8588852253ed6c7bd5b to your computer and use it in GitHub Desktop.
Save rafirh/9e43bd4ff7f1e8588852253ed6c7bd5b to your computer and use it in GitHub Desktop.
Bocchi Governeor Girl
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int[][] cities = new int[x][y];
int[][] floodMap = new int[x][y];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
cities[i][j] = input.nextInt();
}
}
int xBanjir = input.nextInt();
int yBanjir = input.nextInt();
floodMap[xBanjir][yBanjir] = 1;
boolean isFlooded = true;
while (isFlooded) {
isFlooded = false;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (floodMap[i][j] == 1) {
if (i - 1 >= 0 && cities[i - 1][j] < cities[i][j] && floodMap[i - 1][j] == 0) {
floodMap[i - 1][j] = 1;
isFlooded = true;
}
if (i + 1 < x && cities[i + 1][j] < cities[i][j] && floodMap[i + 1][j] == 0) {
floodMap[i + 1][j] = 1;
isFlooded = true;
}
if (j - 1 >= 0 && cities[i][j - 1] < cities[i][j] && floodMap[i][j - 1] == 0) {
floodMap[i][j - 1] = 1;
isFlooded = true;
}
if (j + 1 < y && cities[i][j + 1] < cities[i][j] && floodMap[i][j + 1] == 0) {
floodMap[i][j + 1] = 1;
isFlooded = true;
}
}
}
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(floodMap[i][j] + " ");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment