package com.company; import java.util.Scanner; public class Monopoly { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] dimensions = scanner.nextLine().split("\\s+"); int rows = Integer.parseInt(dimensions[0]); int cols = Integer.parseInt(dimensions[1]); String[] lines = new String[rows]; for (int i = 0; i < rows; i++) { lines[i] = scanner.nextLine(); } char[][] matrix = fillTheMatrix(lines, rows, cols); int turns = 0; int money = 50; int hotels = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (row % 2 == 0) { char ch = matrix[row][col]; switch (ch) { case 'H': { hotels++; System.out.printf("Bought a hotel for %s." + " Total hotels: %d.\n", money, hotels); money = 0; break; } case 'S': { int mToSpend = (row + 1) * (col + 1); if (money < mToSpend) { System.out.printf("Spent %d money at the shop.\n", money); money = 0; } else { System.out.printf("Spent %d money at the shop.\n", mToSpend); money -= mToSpend; } break; } case 'J': { System.out.printf("Gone to jail at turn %d.\n", turns); turns += 2; money += (hotels * 10) * 2; break; } } } else { char ch = matrix[row][cols - col - 1]; switch (ch) { case 'H': { hotels++; System.out.printf("Bought a hotel for %s." + " Total hotels: %d.\n", money, hotels); money = 0; break; } case 'S': { int mToSpend = (row + 1) * ((cols - col - 1) + 1); if (money < mToSpend) { System.out.printf("Spent %d money at the shop.\n", money); money = 0; } else { System.out.printf("Spent %d money at the shop.\n", mToSpend); money -= mToSpend; } break; } case 'J': { System.out.printf("Gone to jail at turn %d.\n", turns); turns += 2; money += (hotels * 10) * 2; break; } } } turns++; money += (hotels * 10); } } System.out.printf("Turns %d\n", turns); System.out.printf("Money %d", money); } public static char[][] fillTheMatrix(String[] lines, int rows, int cols) { char[][] matrix = new char[rows][]; for (int row = 0; row < rows; row++) { char[] innerArr = new char[cols]; for (int col = 0; col < cols; col++) { innerArr[col] = lines[row].charAt(col); } matrix[row] = innerArr; } return matrix; } }