Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. package L24ExamPrep;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class Ex02Sneaking {
  9.     private static Map<Character, int[]> offsets = new HashMap<>() {{
  10.         put('U', new int[] {- 1, 0});
  11.         put('D', new int[] {1, 0});
  12.         put('R', new int[] {0, 1});
  13.         put('L', new int[] {0, - 1});
  14.         put('W', new int[] {0, 0});
  15.     }};
  16.    
  17.     private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.     private static Cell sam;
  19.     private static Cell nicoladze;
  20.     private static int cols;
  21.     private static char[][] maze;
  22.     private static List<Cell> enemies = new ArrayList<>();
  23.    
  24.  
  25.     public static void main(String[] args) throws IOException {
  26.         initMaze();
  27.  
  28.         char[] commands = reader.readLine().toCharArray();
  29.         for (int i = 0; i < commands.length; i++) {
  30.             char command = commands[i];
  31.             moveEnemies();
  32.  
  33.             if (samIsKilled()) {
  34.                 System.out.println(String.format("Sam died at %d, %d", sam.getRow(), sam.getCol()));
  35.                 break;
  36.             }
  37.  
  38.             moveSam(command);
  39.  
  40.             isEnemyDead();
  41.  
  42.             if (isNicoladzeDead()) {
  43.                 System.out.println("Nikoladze killed!");
  44.                 break;
  45.             }
  46.         }
  47.  
  48.         printResult();
  49.     }
  50.  
  51.     private static void printResult() {
  52.         maze[sam.getRow()][sam.getCol()] = sam.getValue();
  53.         maze[nicoladze.getRow()][nicoladze.getCol()] = nicoladze.getValue();
  54.         enemies.forEach(cell -> maze[cell.getRow()][cell.getCol()] = cell.getValue());
  55.  
  56.         for (int row = 0; row < maze.length; row++) {
  57.             StringBuilder rowOutput = new StringBuilder();
  58.             for (int col = 0; col < maze[row].length; col++) {
  59.                 rowOutput.append(maze[row][col]);
  60.             }
  61.             System.out.println(rowOutput.toString());
  62.         }
  63.     }
  64.  
  65.  
  66.     private static boolean isNicoladzeDead() {
  67.         if (sam.getRow() == nicoladze.getRow()) {
  68.             nicoladze.setValue('X');
  69.             return true;
  70.         }
  71.         return false;
  72.     }
  73.  
  74.     private static boolean samIsKilled() {
  75.         Cell enemy = enemies.stream()
  76.                 .filter(cell -> cell.getRow() == sam.getRow())
  77.                 .findAny()
  78.                 .orElse(null);
  79.  
  80.         if (enemy == null) {
  81.             return false;
  82.         }
  83.  
  84.         if (enemy.getCol() < sam.getCol() && enemy.getValue() == 'b') {
  85.             sam.setValue('X');
  86.             return true;
  87.         }
  88.  
  89.         if (enemy.getCol() > sam.getCol() && enemy.getValue() == 'd') {
  90.             sam.setValue('X');
  91.             return true;
  92.         }
  93.  
  94.         return false;
  95.     }
  96.  
  97.     private static void moveSam(char command) {
  98.         int[] offset = offsets.get(command);
  99.         sam.setRow(sam.getRow() + offset[0]);
  100.         sam.setCol(sam.getCol() + offset[1]);
  101.     }
  102.  
  103.     private static void isEnemyDead() {
  104.         enemies.remove(sam);
  105.     }
  106.  
  107.     private static void moveEnemies() {
  108.         for (Cell enemy : enemies) {
  109.             if (enemy.getCol() == 0 && enemy.getValue() == 'd') {
  110.                 enemy.setValue('b');
  111.                 continue;
  112.             }
  113.  
  114.             if (enemy.getCol() == cols - 1 && enemy.getValue() == 'b') {
  115.                 enemy.setValue('d');
  116.                 continue;
  117.             }
  118.  
  119.             char value = enemy.getValue();
  120.             int[] offset = offsets.get(getDirection(value));
  121.             int newColl = enemy.getCol() + offset[1];
  122.             enemy.setCol(newColl);
  123.         }
  124.     }
  125.  
  126.     private static char getDirection(char value) {
  127.         return value == 'b' ? 'R' : 'L';
  128.     }
  129.  
  130.     private static void initMaze() throws IOException {
  131.         int rows = Integer.parseInt(reader.readLine());
  132.         maze = new char[rows][];
  133.         for (int row = 0; row < rows; row++) {
  134.             char[] input = reader.readLine().toCharArray();
  135.             maze[row] = new char[input.length];
  136.             Arrays.fill(maze[row], '.');
  137.             for (int col = 0; col < input.length; col++) {
  138.                 char ch = input[col];
  139.                 if (ch == 'S') {
  140.                     sam = new Cell(row, col, ch);
  141.                 } else if (ch == 'N') {
  142.                     nicoladze = new Cell(row, col, ch);
  143.                 } else if (ch == 'b' || ch == 'd') {
  144.                     enemies.add(new Cell(row, col, ch));
  145.                 }
  146.             }
  147.             cols = input.length;
  148.         }
  149.     }
  150.  
  151.     private static class Cell {
  152.         private int row;
  153.         private int col;
  154.         private char value;
  155.  
  156.         public Cell(int row, int col, char value) {
  157.             this.value = value;
  158.             this.row = row;
  159.             this.col = col;
  160.         }
  161.  
  162.         public int getRow() {
  163.             return this.row;
  164.         }
  165.  
  166.         public int getCol() {
  167.             return this.col;
  168.         }
  169.  
  170.         public char getValue() {
  171.             return this.value;
  172.         }
  173.  
  174.         public void setRow(int row) {
  175.             this.row = row;
  176.         }
  177.  
  178.         public void setCol(int col) {
  179.             this.col = col;
  180.         }
  181.  
  182.         public void setValue(char value) {
  183.             this.value = value;
  184.         }
  185.  
  186.         @Override
  187.         public boolean equals(Object o) {
  188.             if (this == o) return true;
  189.             if (! (o instanceof Cell)) return false;
  190.             Cell cell = (Cell) o;
  191.             return row == cell.row &&
  192.                     col == cell.col;
  193.         }
  194.  
  195.         @Override
  196.         public int hashCode() {
  197.             return Objects.hash(row, col);
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement