SHARE
TWEET

Untitled

mirozspace Jun 19th, 2019 61 in 9 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Pr2TronRacers {
  2.     public static void main(String[] args) {
  3.         Scanner scanner = new Scanner(System.in);
  4.  
  5.         int n = Integer.parseInt(scanner.nextLine());
  6.  
  7.         String[][] matrix = new String[n][n];
  8.  
  9.         for (int i = 0; i < n; i++) {
  10.             String input = scanner.nextLine();
  11.             for (int j = 0; j < n; j++) {
  12.                 matrix[i][j] = input.charAt(j) + "";
  13.             }
  14.         }
  15.         boolean isHasFirstPlayer = false;
  16.         int indexRowFirstPlayer = 0;
  17.         int indexColFirstPlayer = 0;
  18.         for (int i = 0; i < matrix.length; i++) {
  19.             List<String> temp = new ArrayList<>();
  20.             Collections.addAll(temp, matrix[i]);
  21.             if (temp.contains("f")) {
  22.                 indexColFirstPlayer = temp.indexOf("f");
  23.                 indexRowFirstPlayer = i;
  24.                 isHasFirstPlayer = true;
  25.                 break;
  26.             }
  27.         }
  28.         boolean isHasSecondPlayer = false;
  29.         int indexRowSecondPlayer = 0;
  30.         int indexColSecondPlayer = 0;
  31.         for (int i = 0; i < matrix.length; i++) {
  32.             List<String> temp = new ArrayList<>();
  33.             Collections.addAll(temp, matrix[i]);
  34.             if (temp.contains("s")) {
  35.                 indexColSecondPlayer = temp.indexOf("s");
  36.                 indexRowSecondPlayer = i;
  37.                 isHasSecondPlayer = true;
  38.                 break;
  39.             }
  40.         }
  41.         while (isHasFirstPlayer && isHasSecondPlayer) {
  42.  
  43.             String[] commands = scanner.nextLine().split("\\s+");
  44.             String comandForFirst = commands[0];
  45.             String comandForSecond = commands[1];
  46.  
  47.  
  48.             if (comandForFirst.equals("up")) {
  49.                 indexRowFirstPlayer--;
  50.                 if (indexRowFirstPlayer < 0) {
  51.                     indexRowFirstPlayer = matrix.length - 1;
  52.                 }
  53.                 if (matrix[indexRowFirstPlayer][indexColFirstPlayer].equals("s")) {
  54.                     matrix[indexRowFirstPlayer][indexColFirstPlayer] = "x";
  55.                     break;
  56.                 }
  57.                 matrix[indexRowFirstPlayer][indexColFirstPlayer] = "f";
  58.             } else if (comandForFirst.equals("down")) {
  59.                 indexRowFirstPlayer++;
  60.                 if (indexRowFirstPlayer > matrix.length - 1) {
  61.                     indexRowFirstPlayer = 0;
  62.                 }
  63.                 if (matrix[indexRowFirstPlayer][indexColFirstPlayer].equals("s")) {
  64.                     matrix[indexRowFirstPlayer][indexColFirstPlayer] = "x";
  65.                     break;
  66.                 }
  67.                 matrix[indexRowFirstPlayer][indexColFirstPlayer] = "f";
  68.                
  69.             } else if (comandForFirst.equals("left")) {
  70.                 indexColFirstPlayer--;
  71.                 if (indexColFirstPlayer < 0) {
  72.                     indexColFirstPlayer = matrix[indexRowFirstPlayer].length - 1;
  73.                 }
  74.                 if (matrix[indexRowFirstPlayer][indexColFirstPlayer].equals("s")) {
  75.                     matrix[indexRowFirstPlayer][indexColFirstPlayer] = "x";
  76.                     break;
  77.                 }
  78.                 matrix[indexRowFirstPlayer][indexColFirstPlayer] = "f";
  79.  
  80.             } else if (comandForFirst.equals("right")) {
  81.                 indexColFirstPlayer++;
  82.                 if (indexColFirstPlayer > matrix[indexRowFirstPlayer].length - 1) {
  83.                     indexColFirstPlayer = 0;
  84.                 }
  85.                 if (matrix[indexRowFirstPlayer][indexColFirstPlayer].equals("s")) {
  86.                     matrix[indexRowFirstPlayer][indexColFirstPlayer] = "x";
  87.                     break;
  88.                 }
  89.                 matrix[indexRowFirstPlayer][indexColFirstPlayer] = "f";
  90.             }
  91.  
  92.             if (comandForSecond.equals("up")) {
  93.                 indexRowSecondPlayer--;
  94.                 if (indexRowSecondPlayer < 0) {
  95.                     indexRowSecondPlayer = matrix.length - 1;
  96.                 }
  97.                 if (matrix[indexRowSecondPlayer][indexColSecondPlayer].equals("f")) {
  98.                     matrix[indexRowSecondPlayer][indexColSecondPlayer] = "x";
  99.                     break;
  100.                 }
  101.                 matrix[indexRowSecondPlayer][indexColSecondPlayer] = "s";
  102.             } else if (comandForSecond.equals("down")) {
  103.                 indexRowSecondPlayer++;
  104.                 if (indexRowSecondPlayer > matrix.length - 1) {
  105.                     indexRowSecondPlayer = 0;
  106.                 }
  107.                 if (matrix[indexRowSecondPlayer][indexColSecondPlayer].equals("f")) {
  108.                     matrix[indexRowSecondPlayer][indexColSecondPlayer] = "x";
  109.                     break;
  110.                 }
  111.                 matrix[indexRowSecondPlayer][indexColSecondPlayer] = "s";
  112.             } else if (comandForSecond.equals("left")) {
  113.                 indexColSecondPlayer--;
  114.                 if (indexColSecondPlayer < 0) {
  115.                     indexColSecondPlayer = matrix[indexRowSecondPlayer].length - 1;
  116.                 }
  117.                 if (matrix[indexRowSecondPlayer][indexColSecondPlayer].equals("f")) {
  118.                     matrix[indexRowSecondPlayer][indexColSecondPlayer] = "x";
  119.                     break;
  120.                 }
  121.                 matrix[indexRowSecondPlayer][indexColSecondPlayer] = "s";
  122.             } else if (comandForSecond.equals("right")) {
  123.                 indexColSecondPlayer++;
  124.                 if (indexColSecondPlayer > matrix[indexRowSecondPlayer].length - 1) {
  125.                     indexColSecondPlayer = 0;
  126.                 }
  127.                 if (matrix[indexRowSecondPlayer][indexColSecondPlayer].equals("f")) {
  128.                     matrix[indexRowSecondPlayer][indexColSecondPlayer] = "x";
  129.                     break;
  130.                 }
  131.                 matrix[indexRowSecondPlayer][indexColSecondPlayer] = "s";
  132.             }
  133.         }
  134.         for (int i = 0; i < matrix.length; i++) {
  135.             for (int j = 0; j < matrix[i].length; j++) {
  136.                 System.out.print(matrix[i][j]);
  137.             }
  138.             System.out.println();
  139.         }
  140.     }
  141. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top