Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- int[][] population = {{1, 2, 3}, {2, 3, 4}, {3, 2, 1}};
- int x = 0; int y = 0, strength = 2;
- printPopulation(answer(population, x, y, strength));
- }
- private static int[][] answer(int[][] population, int x, int y, int strength) {
- checkInfected(population, x, y, strength);
- return population;
- }
- private static void checkSurroundings(int[][] population, int x, int y, int strength) {
- if(x != 0)
- checkInfected(population, x-1, y, strength);
- if(y != 0)
- checkInfected(population, x, y-1, strength);
- if(x != population[y].length)
- checkInfected(population, x+1, y, strength);
- if(y != population.length)
- checkInfected(population, x, y+1, strength);
- }
- private static void checkInfected(int[][] population, int x, int y, int strength) {
- if(population[y][x] <= strength) {
- population[y][x] = -1;
- checkSurroundings(population, x, y, strength);
- }
- }
- private static void printPopulation(int[][] population) {
- for (int[] row : population) {
- for (int val : row) {
- System.out.print("|" + val + "|");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement