mellowdeep

Untitled

May 4th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. 1
  2. import java.util.ArrayList;
  3. 2
  4. import java.util.List;
  5. 3
  6. import java.util.Scanner;
  7. 4
  8.  
  9. 5
  10. public class _20151004_DragonTrap {
  11. 6
  12.  
  13. 7
  14.     private static List<Point> coordinates = new ArrayList<>();
  15. 8
  16.     private static List<Character> charsToRotate = new ArrayList<>();
  17. 9
  18.     private static char[][] originalMatrix;
  19. 10
  20.     private static char[][] matrix;
  21. 11
  22.    
  23. 12
  24.  
  25. 13
  26.     public static void main(String[] args) {
  27. 14
  28.         Scanner scanner = new Scanner(System.in);
  29. 15
  30.  
  31. 16
  32.         int numberOfRows = Integer.parseInt(scanner.nextLine());
  33. 17
  34.         matrix = new char[numberOfRows][numberOfRows];
  35. 18
  36.         originalMatrix = new char[numberOfRows][numberOfRows];
  37. 19
  38.        
  39. 20
  40.         fillMatrix(numberOfRows, scanner);
  41. 21
  42.                
  43. 22
  44.         String inputLine = scanner.nextLine();
  45. 23
  46.         while (true) {
  47. 24
  48.             if (inputLine.equals("End")) {
  49. 25
  50.                 break;
  51. 26
  52.             }
  53. 27
  54.             String[] splitedLine = inputLine.replaceAll("[()]", "").split(" ");
  55. 28
  56.             int row = Integer.parseInt(splitedLine[0]);
  57. 29
  58.             int col = Integer.parseInt(splitedLine[1]);
  59. 30
  60.             int radius = Integer.parseInt(splitedLine[2]);
  61. 31
  62.             int rotation = Integer.parseInt(splitedLine[3]);
  63. 32
  64.  
  65. 33
  66.             getChars(row, col, radius, rotation);
  67. 34
  68.             rotateCollectedChars(rotation);
  69. 35
  70.             collectRotatedChars();
  71. 36
  72.            
  73. 37
  74.             inputLine = scanner.nextLine();
  75. 38
  76.         }
  77. 39
  78.         int chars = countDifferentChars();
  79. 40
  80.         printMatrix(chars);
  81. 41
  82.     }
  83. 42
  84.  
  85. 43
  86.     private static void printMatrix(int chars) {
  87. 44
  88.         for (int i = 0; i < matrix.length; i++) {
  89. 45
  90.             System.out.println(matrix[i]);
  91. 46
  92.         }
  93. 47
  94.         System.out.printf("Symbols changed: %d",chars);
  95. 48
  96.     }
  97. 49
  98.    
  99. 50
  100.     private static int countDifferentChars() {
  101. 51
  102.         int countDiffChars = 0;
  103. 52
  104.         for (int i = 0; i < originalMatrix.length; i++) {
  105. 53
  106.             for (int j = 0; j < originalMatrix[i].length; j++) {
  107. 54
  108.                 if (matrix[i][j] != originalMatrix[i][j]) {
  109. 55
  110.                     countDiffChars++;
  111. 56
  112.                 }
  113. 57
  114.             }
  115. 58
  116.         }
  117. 59
  118.         return countDiffChars;
  119. 60
  120.     }
  121. 61
  122.  
  123. 62
  124.     private static void collectRotatedChars() {
  125. 63
  126.         int index = 0;
  127. 64
  128.         for (Point point : coordinates) {
  129. 65
  130.             matrix[point.getX()][point.getY()] = charsToRotate.get(index);
  131. 66
  132.             index++;
  133. 67
  134.         }
  135. 68
  136.         charsToRotate.clear();
  137. 69
  138.         coordinates.clear();
  139. 70
  140.     }
  141. 71
  142.  
  143. 72
  144.     private static void rotateCollectedChars(int rotation) {
  145. 73
  146.         if (charsToRotate.size() == 0) {
  147. 74
  148.             return;
  149. 75
  150.         }
  151. 76
  152.         int timesToRotate = Math.abs(rotation) % charsToRotate.size();
  153. 77
  154.         if (rotation >= 0) {
  155. 78
  156.             for (int i = 0; i < timesToRotate; i++) {
  157. 79
  158.                 char last = charsToRotate.get(charsToRotate.size() - 1);
  159. 80
  160.                 charsToRotate.remove(charsToRotate.size() - 1);
  161. 81
  162.                 charsToRotate.add(0, last);
  163. 82
  164.             }
  165. 83
  166.         }else{
  167. 84
  168.             for (int i = 0; i < timesToRotate; i++) {
  169. 85
  170.                 char first = charsToRotate.get(0);
  171. 86
  172.                 charsToRotate.remove(0);
  173. 87
  174.                 charsToRotate.add(first);
  175. 88
  176.             }
  177. 89
  178.         }
  179. 90
  180.     }
  181. 91
  182.  
  183. 92
  184.     private static void getChars(int row, int col, int radius, int rotation) {
  185. 93
  186.         int startRow = row - radius;
  187. 94
  188.         int endRow = row + radius;
  189. 95
  190.         int startCol = col - radius;
  191. 96
  192.         int endCol = col + radius;
  193. 97
  194.  
  195. 98
  196.         for (int i = startCol; i <= endCol; i++) {
  197. 99
  198.             Point point = new Point(startRow, i);
  199. 100
  200.             if (isInsideTheMatrix(point)) {
  201. 101
  202.                 coordinates.add(point);
  203. 102
  204.                 charsToRotate.add(matrix[point.getX()][point.getY()]);
  205. 103
  206.  
  207. 104
  208.             }
  209. 105
  210.         }
  211. 106
  212.  
  213. 107
  214.         for (int i = startRow+1; i < endRow; i++) {
  215. 108
  216.             Point point = new Point(i, endCol);
  217. 109
  218.             if (isInsideTheMatrix(point)) {
  219. 110
  220.                 coordinates.add(point);
  221. 111
  222.                 charsToRotate.add(matrix[point.getX()][point.getY()]);
  223. 112
  224.  
  225. 113
  226.             }
  227. 114
  228.         }
  229. 115
  230.  
  231. 116
  232.         for (int i = endCol; i >= startCol; i--) {
  233. 117
  234.             Point point = new Point(endRow, i);
  235. 118
  236.             if (isInsideTheMatrix(point)) {
  237. 119
  238.                 coordinates.add(point);
  239. 120
  240.                 charsToRotate.add(matrix[point.getX()][point.getY()]);
  241. 121
  242.  
  243. 122
  244.             }
  245. 123
  246.         }
  247. 124
  248.         for (int i = endRow-1; i > startRow; i--) {
  249. 125
  250.             Point point = new Point(i, startCol);
  251. 126
  252.             if (isInsideTheMatrix(point)) {
  253. 127
  254.                 coordinates.add(point);
  255. 128
  256.                 charsToRotate.add(matrix[point.getX()][point.getY()]);
  257. 129
  258.  
  259. 130
  260.             }
  261. 131
  262.         }
  263. 132
  264.     }
  265. 133
  266.  
  267. 134
  268.     private static boolean isInsideTheMatrix(Point point) {
  269. 135
  270.         return point.getX() < matrix.length
  271. 136
  272.                 && point.getX() >= 0
  273. 137
  274.                 && point.getY() < matrix[point.getX()].length
  275. 138
  276.                 && point.getY() >= 0;
  277. 139
  278.     }
  279. 140
  280.  
  281. 141
  282.     private static void fillMatrix(int numberOfRows, Scanner scanner) {
  283. 142
  284.         for (int i = 0; i < numberOfRows; i++) {
  285. 143
  286.             String input = scanner.nextLine();
  287. 144
  288.             originalMatrix[i] = input.toCharArray();
  289. 145
  290.             matrix[i] = input.toCharArray();
  291. 146
  292.         }
  293. 147
  294.     }
  295. 148
  296. }
  297. 149
  298.  
  299. 150
  300. class Point{
  301. 151
  302.    
  303. 152
  304.     private int x;
  305. 153
  306.     private int y;
  307. 154
  308.    
  309. 155
  310.     public Point(int x, int y) {
  311. 156
  312.         super();
  313. 157
  314.         this.x = x;
  315. 158
  316.         this.y = y;
  317. 159
  318.     }
  319. 160
  320.  
  321. 161
  322.     public int getX() {
  323. 162
  324.         return x;
  325. 163
  326.     }
  327. 164
  328.  
  329. 165
  330.     public int getY() {
  331. 166
  332.         return y;
  333. 167
  334.     }
  335. 168
  336.  
  337. 169
  338.     @Override
  339. 170
  340.     public String toString() {
  341. 171
  342.         return "Point [x=" + x + ", y=" + y + "]";
  343. 172
  344.     }
  345. 173
  346.    
  347. 174
  348. }
Advertisement
Add Comment
Please, Sign In to add comment