Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package repository;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Main {
- private static int rowStephen;
- private static int colStephen;
- public static void main(String[] args) throws IOException {
- BufferedReader inputReader =
- new BufferedReader(
- new InputStreamReader(System.in));
- int rows = Integer.parseInt(inputReader.readLine());
- char[][] galaxy = new char[rows][];
- for (int r = 0; r < galaxy.length; r++) {
- String inputLine = inputReader.readLine();
- galaxy[r] = new char[inputLine.length()];
- for (int c = 0; c < inputLine.length(); c++) {
- galaxy[r][c] = inputLine.charAt(c);
- }
- }
- int rowFirstHole = -2;
- int colFirstHole = -2;
- boolean foundHole = false;
- for (int r = 0; r < galaxy.length; r++) {
- if (foundHole) {
- break;
- }
- for (int c = 0; c < galaxy[r].length; c++) {
- if (galaxy[r][c] == 'O') {
- rowFirstHole = r;
- colFirstHole = c;
- foundHole = true;
- break;
- }
- }
- }
- int rowSecondHole = -2;
- int colSecondHole = -2;
- if (rowFirstHole != -2) {
- for (int r = rowFirstHole; r < galaxy.length; r++) {
- for (int c = colFirstHole; c < galaxy[r].length; c++) {
- if (galaxy[r][c] == 'O') {
- rowSecondHole = r;
- colSecondHole = c;
- }
- }
- }
- }
- rowStephen = 0;
- colStephen = 0;
- for (int r = 0; r < galaxy.length; r++) {
- for (int c = 0; c < galaxy[r].length; c++) {
- if (galaxy[r][c] == 'S') {
- rowStephen = r;
- colStephen = c;
- }
- }
- }
- int starPower = 0;
- boolean outOfField = false;
- while (!outOfField && starPower < 50) {
- String command = inputReader.readLine();
- switch (command) {
- case "up":
- if (rowStephen - 1 < 0) {
- outOfField = true;
- galaxy[rowStephen][colStephen] = '-';
- } else if (Character.isDigit(galaxy[rowStephen - 1][colStephen])) {
- starPower += Integer.parseInt(String.valueOf(galaxy[rowStephen - 1][colStephen]));
- galaxy[rowStephen][colStephen] = '-';
- rowStephen--;
- galaxy[rowStephen][colStephen] = 'S';
- } else if (galaxy[rowStephen - 1][colStephen] == 'O') {
- if (colStephen == colFirstHole && rowStephen - 1 == rowFirstHole) {
- moveFirstToSecond(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- } else if (colStephen == colSecondHole && rowStephen - 1 == rowSecondHole) {
- moveSecondToFirst(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- }
- } else {
- galaxy[rowStephen][colStephen] = '-';
- rowStephen--;
- galaxy[rowStephen][colStephen] = 'S';
- }
- break;
- case "down":
- if (rowStephen + 1 > galaxy.length - 1) {
- outOfField = true;
- galaxy[rowStephen][colStephen] = '-';
- } else if (Character.isDigit(galaxy[rowStephen + 1][colStephen])) {
- starPower += Integer.parseInt(String.valueOf(galaxy[rowStephen + 1][colStephen]));
- galaxy[rowStephen][colStephen] = '-';
- rowStephen++;
- galaxy[rowStephen][colStephen] = 'S';
- } else if (galaxy[rowStephen + 1][colStephen] == 'O') {
- if (colStephen == colFirstHole && rowStephen + 1 == rowFirstHole) {
- moveFirstToSecond(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- } else if (colStephen == colSecondHole && rowStephen + 1 == rowSecondHole) {
- moveSecondToFirst(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- }
- } else {
- galaxy[rowStephen][colStephen] = '-';
- rowStephen++;
- galaxy[rowStephen][colStephen] = 'S';
- }
- break;
- case "left":
- if (colStephen - 1 < 0) {
- outOfField = true;
- galaxy[rowStephen][colStephen] = '-';
- } else if (Character.isDigit(galaxy[rowStephen][colStephen - 1])) {
- starPower += Integer.parseInt(String.valueOf(galaxy[rowStephen][colStephen - 1]));
- galaxy[rowStephen][colStephen] = '-';
- colStephen--;
- galaxy[rowStephen][colStephen] = 'S';
- } else if (galaxy[rowStephen][colStephen - 1] == 'O') {
- if (colStephen - 1 == colFirstHole && rowStephen == rowFirstHole) {
- moveFirstToSecond(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- } else if (colStephen - 1 == colSecondHole && rowStephen == rowSecondHole) {
- moveSecondToFirst(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- }
- } else {
- galaxy[rowStephen][colStephen] = '-';
- colStephen--;
- galaxy[rowStephen][colStephen] = 'S';
- }
- break;
- case "right":
- if (colStephen + 1 > galaxy.length - 1) {
- outOfField = true;
- galaxy[rowStephen][colStephen] = '-';
- } else if (Character.isDigit(galaxy[rowStephen][colStephen + 1])) {
- starPower += Integer.parseInt(String.valueOf(galaxy[rowStephen][colStephen + 1]));
- galaxy[rowStephen][colStephen] = '-';
- colStephen++;
- galaxy[rowStephen][colStephen] = 'S';
- } else if (galaxy[rowStephen][colStephen + 1] == 'O') {
- if (colStephen + 1 == colFirstHole && rowStephen == rowFirstHole) {
- moveFirstToSecond(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- } else if (colStephen + 1 == colSecondHole && rowStephen == rowSecondHole) {
- moveSecondToFirst(galaxy, rowFirstHole, colFirstHole, rowSecondHole, colSecondHole);
- }
- } else {
- galaxy[rowStephen][colStephen] = '-';
- colStephen++;
- galaxy[rowStephen][colStephen] = 'S';
- }
- break;
- }
- }
- if (outOfField) {
- System.out.println("Bad news, the spaceship went to the void.");
- System.out.printf("Star power collected: %d%n", starPower);
- } else if (starPower >= 50) {
- System.out.println("Good news! Stephen succeeded in collecting enough star power!");
- System.out.printf("Star power collected: %d%n", starPower);
- }
- for (int r = 0; r < galaxy.length; r++) {
- for (int c = 0; c < galaxy[r].length; c++) {
- System.out.print(galaxy[r][c]);
- }
- System.out.println();
- }
- }
- private static void moveSecondToFirst(char[][] galaxy,
- int rowFirstHole, int colFirstHole,
- int rowSecondHole, int colSecondHole) {
- galaxy[rowStephen][colStephen] = '-';
- galaxy[rowSecondHole][colSecondHole] = '-';
- galaxy[rowFirstHole][colFirstHole] = 'S';
- rowStephen = rowFirstHole;
- colStephen = colFirstHole;
- }
- private static void moveFirstToSecond(char[][] galaxy,
- int rowFirstHole, int colFirstHole,
- int rowSecondHole, int colSecondHole) {
- galaxy[rowStephen][colStephen] = '-';
- galaxy[rowFirstHole][colFirstHole] = '-';
- galaxy[rowSecondHole][colSecondHole] = 'S';
- rowStephen = rowSecondHole;
- colStephen = colSecondHole;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement