Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.73 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import java.io.IOException;
  7.  
  8. public class Sneaking {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         int lineNum = Integer.parseInt(reader.readLine());
  13.         double NikolazPosition = 0;
  14.         double SamPosition = 0;
  15.  
  16.  
  17.  
  18.         List<String> line = Arrays.stream(reader.readLine().split("")).collect(Collectors.toList());
  19.         String[][] matrix = new String[lineNum][line.size()];
  20.         int rowCount = 0;
  21.         int colCount = line.size();
  22.         lineNum--;
  23.         while(lineNum-- >= 0){
  24.             for (int col = 0; col < line.size(); col++) {
  25.                 matrix[rowCount][col] = line.get(col);
  26.                 if (line.get(col).equals("N")) {
  27.                     NikolazPosition = Double.parseDouble(rowCount + "." + col);
  28.                 }
  29.                 if (line.get(col).equals("S")) {
  30.                     SamPosition = Double.parseDouble(rowCount + "." + col);
  31.                 }
  32.             }
  33.             rowCount ++;
  34.  
  35.             line = Arrays.stream(reader.readLine().split("")).collect(Collectors.toList());
  36.         }
  37.         List<String> commands = line;
  38.         while(commands.size()+1 > 0 ){
  39.  
  40.             if(SamPosition < 0){
  41.                 System.out.println("Sam died at " + (int)SamPosition*(-1) + ", " + (int)((SamPosition*(-10))%10));
  42.                 PrintMatrix(rowCount,colCount,matrix);
  43.                 return;
  44.             }
  45.             else if((int)SamPosition == (int)NikolazPosition){
  46.                 System.out.println("Nikoladze killed!");
  47.                 matrix[(int)(NikolazPosition)][(int)((NikolazPosition*10)%10)] = "X";
  48.                 PrintMatrix(rowCount,colCount,matrix);
  49.                 return;
  50.             }
  51.             else {
  52.             String currCommand = commands.get(0);
  53.             int rount = 0;
  54.             MoveEnemy(rowCount, colCount, matrix);
  55.             SamPosition = SamMove(colCount, currCommand, SamPosition, matrix);
  56.             commands.remove(0);
  57.             }
  58.         }
  59.     }
  60.  
  61.     private static void PrintMatrix(int lineCount, int colCount, String[][] newMatrix) {
  62.         for (int row = 0; row < lineCount ; row++) {
  63.             for (int col = 0; col < colCount; col++) {
  64.                 System.out.print(newMatrix[row][col]);
  65.             }
  66.             System.out.println();
  67.         }
  68.     }
  69.  
  70.     private static void MoveEnemy(int lineCount, int colCount, String[][] matrix) {
  71.         for (int row = 0; row < lineCount; row++) {
  72.             for (int col = 0; col < colCount; col++) {
  73.                if( matrix[row][col].equals("b")){
  74.                    if(col+1 >= colCount){
  75.                        matrix[row][col]= "d";
  76.                    }
  77.                    else{
  78.                        matrix[row][col] = ".";
  79.                        matrix[row][col+1]= "b";
  80.                    }
  81.                    break;
  82.                }
  83.                else if( matrix[row][col].equals("d")){
  84.                    if(col-1 < 0){
  85.                        matrix[row][col] = "b";
  86.                    }
  87.                    else{
  88.                        matrix[row][col] = ".";
  89.                        matrix[row][col-1]= "d";
  90.                    }
  91.                    break;
  92.                }
  93.             }
  94.         }
  95.     }
  96.  
  97.     private static double SamMove( int colCount, String direction, double SamCurrPos, String[][] Matrix) {
  98.  
  99.         double CurrPos = 0;
  100.         int xPos = (int)SamCurrPos;
  101.         int yPos = (int)((SamCurrPos*10)%10);
  102.         for(int col =0; col < colCount; col++){
  103.             if(Matrix[xPos][col].equals("b")){
  104.                 if(col < yPos){
  105.                     Matrix[xPos][yPos] = "X";
  106.                     return Double.parseDouble("-" + xPos + "." + yPos);
  107.                 }
  108.             }
  109.             else if(Matrix[xPos][col].equals("d")) {
  110.                 if (col > yPos) {
  111.                     Matrix[xPos][yPos] = "X";
  112.                     return Double.parseDouble("-" + xPos + "." + yPos);
  113.                 }
  114.             }
  115.         }
  116.         Matrix[xPos][yPos] = ".";
  117.         switch(direction){
  118.             case "U":
  119.                 for (int col = 0; col < colCount; col++) {
  120.                    if(Matrix[xPos-1][col].equals("b")){
  121.                        if(col < yPos){
  122.                            Matrix[xPos-1][yPos] = "X";
  123.                            CurrPos = Double.parseDouble("-" + (xPos-1) + "." + yPos);
  124.                            break;
  125.                        }
  126.                        else if(col == yPos){
  127.                            Matrix[xPos+1][yPos] = "S";
  128.                            break;
  129.                        }
  130.                    }
  131.                    else if(Matrix[xPos-1][col].equals("d")){
  132.                        if(col > yPos){
  133.                            Matrix[xPos-1][yPos] = "X";
  134.                            CurrPos = Double.parseDouble("-" + (xPos-1) + "." + yPos);
  135.                            break;
  136.                        }
  137.                        else if(col == yPos){
  138.                            Matrix[xPos-1][yPos] = "S";
  139.                            break;
  140.                        }
  141.                    }
  142.                    else{
  143.                        Matrix[xPos-1][yPos] = "S";
  144.                        CurrPos = Double.parseDouble((xPos-1) + "." + yPos);
  145.                        break;
  146.                    }
  147.                 }
  148.                 break;
  149.             case "D":
  150.                 for (int col = 0; col < colCount; col++) {
  151.                     if(Matrix[xPos+1][col].equals("b")){
  152.                         if(col < yPos){
  153.                             Matrix[xPos+1][yPos] = "X";
  154.                             CurrPos = Double.parseDouble("-" + (xPos+1) + "." + yPos);
  155.                             break;
  156.                         }
  157.                         else if(col == yPos){
  158.                             Matrix[xPos-1][yPos] = "S";
  159.                             break;
  160.                         }
  161.                     }
  162.                     else if(Matrix[xPos+1][col].equals("d")){
  163.                         if(col > yPos){
  164.                             Matrix[xPos+1][yPos] = "X";
  165.                             CurrPos = Double.parseDouble("-" + (xPos+1) + "." + yPos);
  166.                             break;
  167.                         }
  168.                         else if(col == yPos){
  169.                             Matrix[xPos+1][yPos] = "S";
  170.                             break;
  171.                         }
  172.                     }
  173.                     else{
  174.                         Matrix[xPos+1][yPos] = "S";
  175.                         CurrPos = Double.parseDouble((xPos+1) + "." + yPos);
  176.                         break;
  177.                     }
  178.                 }
  179.                 break;
  180.             case "L":
  181.                 for (int col = 0; col < colCount; col++) {
  182.                     if (Matrix[xPos][col].equals("b")) {
  183.                         if (col < yPos) {
  184.                             Matrix[xPos][yPos-1] = "X";
  185.                             CurrPos = Double.parseDouble("-" + xPos + "." + (yPos-1));
  186.                             break;
  187.                         }
  188.                     } else if (Matrix[xPos][col].equals("d")) {
  189.                         if (col > yPos) {
  190.                             Matrix[xPos][yPos-1] = "X";
  191.                             CurrPos = Double.parseDouble("-" + xPos + "." + (yPos-1));
  192.                             break;
  193.                         }
  194.                     } else {
  195.                         Matrix[xPos][yPos-1] = "S";
  196.                         CurrPos = Double.parseDouble(xPos + "." + (yPos-1));
  197.                         break;
  198.                     }
  199.                 }
  200.                 break;
  201.             case "R":
  202.                 for (int col = 0; col < colCount; col++) {
  203.                     if (Matrix[xPos][col].equals("b")) {
  204.                         if (col < yPos) {
  205.                             Matrix[xPos][yPos+1] = "X";
  206.                             CurrPos = Double.parseDouble("-" + xPos + "." + (yPos+1));
  207.                             break;
  208.                         }
  209.                     } else if (Matrix[xPos][col].equals("d")) {
  210.                         if (col > yPos) {
  211.                             Matrix[xPos + 1][yPos+1] = "X";
  212.                             CurrPos = Double.parseDouble("-" + xPos + "." + (yPos+1));
  213.                             break;
  214.                         }
  215.                     } else {
  216.                         Matrix[xPos][yPos+1] = "S";
  217.                         CurrPos = Double.parseDouble(xPos + "." + (yPos+1));
  218.                         break;
  219.                     }
  220.                 }
  221.                 break;
  222.             case "W":
  223.                 for (int col = 0; col < colCount; col++) {
  224.                     if (Matrix[xPos][col].equals("b")) {
  225.                         if (col < yPos) {
  226.                             Matrix[xPos][yPos] = "X";
  227.                             CurrPos = Double.parseDouble("-" + xPos + "." + (yPos));
  228.                             break;
  229.                         }
  230.                     } else if (Matrix[xPos][col].equals("d")) {
  231.                         if (col > yPos) {
  232.                             Matrix[xPos][yPos] = "X";
  233.                             CurrPos = Double.parseDouble("-" + xPos + "." + (yPos));
  234.                             break;
  235.                         }
  236.                     } else {
  237.                         Matrix[xPos][yPos] = "S";
  238.                         CurrPos = Double.parseDouble(xPos + "." + yPos);
  239.                         break;
  240.                     }
  241.                 }
  242.                 break;
  243.             }
  244.             return CurrPos;
  245.         }
  246.  
  247.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement