desislava_topuzakova

Untitled

Oct 25th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Garden {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String[] dimensions = scanner.nextLine().split(" ");
  7. int n = Integer.parseInt(dimensions[0]);
  8. int m = Integer.parseInt(dimensions[1]);
  9.  
  10. int [][] garden = new int[n][m];
  11. for (int row = 0; row < n ; row++) {
  12. for (int col = 0; col < m; col++) {
  13. garden[row][col] = 0;
  14. }
  15. }
  16.  
  17. String input = scanner.nextLine();
  18. while (!input.equals("Bloom Bloom Plow")) {
  19. String[] plantPositions = input.split(" ");
  20. int plantRow = Integer.parseInt(plantPositions[0]);
  21. int plantCol = Integer.parseInt(plantPositions[1]);
  22. try {
  23. validateCoordinates(plantRow, plantCol, n);
  24. } catch (IllegalArgumentException exception) {
  25. System.out.println("Invalid coordinates.");
  26. input = scanner.nextLine();
  27. continue;
  28. }
  29. garden[plantRow][plantCol] = 1;
  30. for (int col = 0; col < m ; col++) {
  31. if(col == plantCol){
  32. continue;
  33. }
  34. garden[plantRow][col] += 1;
  35. }
  36. for (int row = 0; row < n; row++) {
  37. if(row == plantRow){
  38. continue;
  39. }
  40. garden[row][plantCol] += 1;
  41. }
  42.  
  43. input = scanner.nextLine();
  44. }
  45.  
  46. printMatrix(garden);
  47.  
  48. }
  49.  
  50. private static void validateCoordinates(int plantRow, int plantCol, int n) {
  51. if(plantRow >= n || plantRow < 0 || plantCol >= n || plantCol < 0){
  52. throw new IllegalArgumentException();
  53. }
  54. }
  55.  
  56. private static void printMatrix(int[][] garden) {
  57. for (int row = 0; row < garden.length; row++) {
  58. for (int col = 0; col < garden.length; col++) {
  59. System.out.print(garden[row][col] + " ");
  60. }
  61. System.out.println();
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment