Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Garden {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] dimensions = scanner.nextLine().split(" ");
- int n = Integer.parseInt(dimensions[0]);
- int m = Integer.parseInt(dimensions[1]);
- int [][] garden = new int[n][m];
- for (int row = 0; row < n ; row++) {
- for (int col = 0; col < m; col++) {
- garden[row][col] = 0;
- }
- }
- String input = scanner.nextLine();
- while (!input.equals("Bloom Bloom Plow")) {
- String[] plantPositions = input.split(" ");
- int plantRow = Integer.parseInt(plantPositions[0]);
- int plantCol = Integer.parseInt(plantPositions[1]);
- try {
- validateCoordinates(plantRow, plantCol, n);
- } catch (IllegalArgumentException exception) {
- System.out.println("Invalid coordinates.");
- input = scanner.nextLine();
- continue;
- }
- garden[plantRow][plantCol] = 1;
- for (int col = 0; col < m ; col++) {
- if(col == plantCol){
- continue;
- }
- garden[plantRow][col] += 1;
- }
- for (int row = 0; row < n; row++) {
- if(row == plantRow){
- continue;
- }
- garden[row][plantCol] += 1;
- }
- input = scanner.nextLine();
- }
- printMatrix(garden);
- }
- private static void validateCoordinates(int plantRow, int plantCol, int n) {
- if(plantRow >= n || plantRow < 0 || plantCol >= n || plantCol < 0){
- throw new IllegalArgumentException();
- }
- }
- private static void printMatrix(int[][] garden) {
- for (int row = 0; row < garden.length; row++) {
- for (int col = 0; col < garden.length; col++) {
- System.out.print(garden[row][col] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment