Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SpaceStationEstablishment {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- char[][] matrix = new char[n][n];
- int rowIndex = 0;
- int colIndex = 0;
- int counter = 0;
- int firstBlackholeRow = 0;
- int firstBlackholeCol = 0;
- int secondBlackholeRow = 0;
- int secondBlackholeCol = 0;
- for (int i = 0; i < n; i++) {
- String row = scanner.nextLine();
- for (int j = 0; j < row.length(); j++) {
- matrix[i][j] = row.charAt(j);
- if (matrix[i][j] == 'S') {
- rowIndex = i;
- colIndex = j;
- }
- if (matrix[i][j] == 'O') {
- counter++;
- if (counter == 1) {
- firstBlackholeRow = i;
- firstBlackholeCol = j;
- } else {
- secondBlackholeRow = i;
- secondBlackholeCol = j;
- }
- }
- }
- }
- boolean crossBorder = false;
- int starPower = 0;
- String command = scanner.nextLine();
- while (starPower < 50) {
- if (command.equals("left")) {
- if (!crossBorder(command, matrix, rowIndex, colIndex)) {
- if (Character.isDigit(matrix[rowIndex][colIndex - 1])) {
- starPower += Integer.parseInt(matrix[rowIndex][colIndex - 1] + "");
- if (starPower>=50){
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex][colIndex - 1] = 'S';
- break;
- }
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex][colIndex - 1] = 'S';
- colIndex = colIndex - 1;
- } else if (matrix[rowIndex][colIndex - 1] == 'O') {
- if (rowIndex == firstBlackholeRow && colIndex - 1 == firstBlackholeCol) {
- matrix[rowIndex][colIndex] = '-';
- matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
- matrix[firstBlackholeRow][firstBlackholeCol] = '-';
- rowIndex = secondBlackholeRow;
- colIndex = secondBlackholeCol;
- } else {
- matrix[rowIndex][colIndex] = '-';
- matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
- matrix[secondBlackholeRow][secondBlackholeCol] = '-';
- rowIndex = firstBlackholeRow;
- colIndex = firstBlackholeCol;
- }
- } else {
- matrix[rowIndex][colIndex - 1] = 'S';
- matrix[rowIndex][colIndex] = '-';
- colIndex = colIndex - 1;
- }
- } else {
- System.out.println("Bad news, the spaceship went to the void.");
- System.out.println("Star power collected: " + starPower);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- return;
- }
- } else if (command.equals("right")) {
- if (!crossBorder(command, matrix, rowIndex, colIndex)) {
- if (Character.isDigit(matrix[rowIndex][colIndex + 1])) {
- starPower += Integer.parseInt(matrix[rowIndex][colIndex + 1] + "");
- if (starPower>=50){
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex][colIndex + 1] = 'S';
- break;
- }
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex][colIndex + 1] = 'S';
- colIndex = colIndex + 1;
- } else if (matrix[rowIndex][colIndex + 1] == 'O') {
- if (rowIndex == firstBlackholeRow && colIndex + 1 == firstBlackholeCol) {
- matrix[rowIndex][colIndex] = '-';
- matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
- matrix[firstBlackholeRow][firstBlackholeCol] = '-';
- rowIndex = secondBlackholeRow;
- colIndex = secondBlackholeCol;
- } else {
- matrix[rowIndex][colIndex] = '-';
- matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
- matrix[secondBlackholeRow][secondBlackholeCol] = '-';
- rowIndex = firstBlackholeRow;
- colIndex = firstBlackholeCol;
- }
- } else {
- matrix[rowIndex][colIndex + 1] = 'S';
- matrix[rowIndex][colIndex] = '-';
- colIndex = colIndex + 1;
- }
- } else {
- System.out.println("Bad news, the spaceship went to the void.");
- System.out.println("Star power collected: " + starPower);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- return;
- }
- } else if (command.equals("up")) {
- if (!crossBorder(command, matrix, rowIndex, colIndex)) {
- if (Character.isDigit(matrix[rowIndex - 1][colIndex])) {
- starPower += Integer.parseInt(matrix[rowIndex - 1][colIndex] + "");
- if (starPower>=50){
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex - 1][colIndex] = 'S';
- break;
- }
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex - 1][colIndex] = 'S';
- rowIndex = rowIndex - 1;
- } else if (matrix[rowIndex - 1][colIndex] == 'O') {
- if (rowIndex - 1 == firstBlackholeRow && colIndex == firstBlackholeCol) {
- matrix[rowIndex][colIndex] = '-';
- matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
- matrix[firstBlackholeRow][firstBlackholeCol] = '-';
- rowIndex = secondBlackholeRow;
- colIndex = secondBlackholeCol;
- } else {
- matrix[rowIndex][colIndex] = '-';
- matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
- matrix[secondBlackholeRow][secondBlackholeCol] = '-';
- rowIndex = firstBlackholeRow;
- colIndex = firstBlackholeCol;
- }
- } else {
- matrix[rowIndex - 1][colIndex] = 'S';
- matrix[rowIndex][colIndex] = '-';
- rowIndex = rowIndex - 1;
- }
- } else {
- System.out.println("Bad news, the spaceship went to the void.");
- System.out.println("Star power collected: " + starPower);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- return;
- }
- } else if (command.equals("down")) {
- if (!crossBorder(command, matrix, rowIndex, colIndex)) {
- if (Character.isDigit(matrix[rowIndex + 1][colIndex])) {
- starPower += Integer.parseInt(matrix[rowIndex + 1][colIndex] + "");
- if (starPower>=50){
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex + 1][colIndex] = 'S';
- break;
- }
- matrix[rowIndex][colIndex] = '-';
- matrix[rowIndex + 1][colIndex] = 'S';
- rowIndex = rowIndex + 1;
- } else if (matrix[rowIndex + 1][colIndex] == 'O') {
- if (rowIndex + 1 == firstBlackholeRow && colIndex == firstBlackholeCol) {
- matrix[rowIndex][colIndex] = '-';
- matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
- matrix[firstBlackholeRow][firstBlackholeCol] = '-';
- rowIndex = secondBlackholeRow;
- colIndex = secondBlackholeCol;
- } else {
- matrix[rowIndex][colIndex] = '-';
- matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
- matrix[secondBlackholeRow][secondBlackholeCol] = '-';
- rowIndex = firstBlackholeRow;
- colIndex = firstBlackholeCol;
- }
- } else {
- matrix[rowIndex + 1][colIndex] = 'S';
- matrix[rowIndex][colIndex] = '-';
- rowIndex = rowIndex + 1;
- }
- } else {
- System.out.println("Bad news, the spaceship went to the void.");
- System.out.println("Star power collected: " + starPower);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- return;
- }
- }
- command = scanner.nextLine();
- }
- System.out.println("Good news! Stephen succeeded in collecting enough star power!");
- System.out.println("Star power collected: " + starPower);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- }
- public static boolean crossBorder(String command, char[][] matrix, int rowIndex, int colIndex) {
- boolean outOfBorder = false;
- int newRowIndex = 0;
- int newColIndex = 0;
- if (command.equals("left")) {
- newColIndex = colIndex - 1;
- if (newColIndex < 0) {
- outOfBorder = true;
- }
- } else if (command.equals("right")) {
- newColIndex = colIndex + 1;
- if (newColIndex >= matrix.length) {
- outOfBorder = true;
- }
- } else if (command.equals("up")) {
- newRowIndex = rowIndex - 1;
- if (newRowIndex < 0) {
- outOfBorder = true;
- }
- } else if (command.equals("down")) {
- newRowIndex = rowIndex + 1;
- if (newRowIndex >= matrix.length) {
- outOfBorder = true;
- }
- } else {
- outOfBorder = false;
- }
- matrix[rowIndex][colIndex] = '-';
- return outOfBorder;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement