Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public static void main(String[] args) {
  2. int[][] population = {{1, 2, 3}, {2, 3, 4}, {3, 2, 1}};
  3. int x = 0; int y = 0, strength = 2;
  4.  
  5. printPopulation(answer(population, x, y, strength));
  6. }
  7.  
  8. private static int[][] answer(int[][] population, int x, int y, int strength) {
  9. checkInfected(population, x, y, strength);
  10. return population;
  11. }
  12.  
  13. private static void checkSurroundings(int[][] population, int x, int y, int strength) {
  14. if(x != 0)
  15. checkInfected(population, x-1, y, strength);
  16. if(y != 0)
  17. checkInfected(population, x, y-1, strength);
  18. if(x != population[y].length)
  19. checkInfected(population, x+1, y, strength);
  20. if(y != population.length)
  21. checkInfected(population, x, y+1, strength);
  22. }
  23.  
  24. private static void checkInfected(int[][] population, int x, int y, int strength) {
  25. if(population[y][x] <= strength) {
  26. population[y][x] = -1;
  27. checkSurroundings(population, x, y, strength);
  28. }
  29. }
  30.  
  31. private static void printPopulation(int[][] population) {
  32. for (int[] row : population) {
  33. for (int val : row) {
  34. System.out.print("|" + val + "|");
  35. }
  36. System.out.println();
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement