Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class DragonTrap {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int n = Integer.parseInt(scanner.nextLine());
  9.  
  10. char[][] matrix = new char[n][];
  11. char[][] model = new char[n][];
  12.  
  13.  
  14. for (int i = 0; i < n; i++) {
  15. char[] chars = scanner.nextLine().trim().toCharArray();
  16. matrix[i] = chars;
  17. model[i] = chars.clone();
  18. }
  19.  
  20. while (true){
  21. String currentLine = scanner.nextLine().trim();
  22. if (currentLine.equals("End")){
  23. break;
  24. }
  25.  
  26. currentLine = currentLine.replaceAll("[()]+", " ");
  27. String[] splitet = currentLine.split("\\s+");
  28. //String coordinates = splitet[0].substring(0, splitet[0].length());
  29. //String[] splitetCoordinates = coordinates.trim().split("\\s+");
  30.  
  31. int row = Integer.parseInt(splitet[1]);
  32. int col = Integer.parseInt(splitet[2]);
  33. Integer radius = Integer.parseInt(splitet[3]);
  34. Integer numberOfRotations = Integer.parseInt(splitet[4]);
  35.  
  36. if (numberOfRotations > 0){
  37. RotateClockwise(matrix, row, col, radius, numberOfRotations);
  38. } else if (numberOfRotations < 0) {
  39. RotateOpositeClockwise(matrix, row, col, radius, numberOfRotations);
  40. }
  41. }
  42.  
  43. int changes = 0;
  44.  
  45. for (int i = 0; i < matrix.length; i++) {
  46. for (int j = 0; j < matrix[i].length; j++) {
  47. System.out.print(matrix[i][j]);
  48. if (matrix[i][j]!=model[i][j]){
  49. changes++;
  50. }
  51. }
  52. System.out.println();
  53. }
  54.  
  55. System.out.println("Symbols changed: " + changes);
  56. }
  57.  
  58. private static void RotateOpositeClockwise(char[][] matrix, int centerRow, int centerCol, Integer radius, Integer numberOfRotations) {
  59. ArrayList<int[]> positionsToRotate = findValidPositions(matrix, centerRow, centerCol, radius);
  60. if (positionsToRotate.size()==0){
  61. return ;
  62. }
  63.  
  64. for (int j = 0; j < numberOfRotations%positionsToRotate.size(); j++) {
  65. Character prev = matrix[positionsToRotate.get(positionsToRotate.size()-1)[0]][positionsToRotate.get(positionsToRotate.size()-1)[1]];
  66. for (int i = positionsToRotate.size()-2; i >= 0 ; i++) {
  67. Character current = matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]];
  68. matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]] = prev;
  69. prev = current;
  70. }
  71.  
  72. matrix[positionsToRotate.get(positionsToRotate.size()-1)[0]][positionsToRotate.get(positionsToRotate.size()-1)[1]] = prev;
  73. }
  74. }
  75.  
  76. private static void RotateClockwise(char[][] matrix, int centerRow, int centerCol, Integer radius, Integer numberOfRotations) {
  77. ArrayList<int[]> positionsToRotate = findValidPositions(matrix, centerRow, centerCol, radius);
  78. if (positionsToRotate.size()==0){
  79. return ;
  80. }
  81.  
  82.  
  83. for (int j = 0; j < numberOfRotations%positionsToRotate.size(); j++) {
  84. Character prev = matrix[positionsToRotate.get(0)[0]][positionsToRotate.get(0)[1]];
  85. for (int i = 1; i < positionsToRotate.size(); i++) {
  86. Character current = matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]];
  87. matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]] = prev;
  88. prev = current;
  89. }
  90.  
  91. matrix[positionsToRotate.get(0)[0]][positionsToRotate.get(0)[1]] = prev;
  92.  
  93. }
  94. }
  95.  
  96. private static ArrayList<int[]> findValidPositions(char[][] matrix, int centerRow, int centerCol, Integer radius) {
  97. ArrayList<int[]> positions = new ArrayList<>();
  98. if (centerRow-radius >= 0&&centerRow-radius<matrix.length) {
  99. for (int i = centerCol - radius; i <= centerCol + radius; i++) {
  100. positions.add(new int[]{centerRow - radius, i});
  101. }
  102. }
  103. if (centerCol+radius>=0&&centerCol<matrix[0].length) {
  104. for (int i = centerRow - radius + 1; i <= centerRow + radius; i++) {
  105. positions.add(new int[]{i, centerCol + radius});
  106. }
  107. }
  108. if (centerRow+radius >= 0&&centerRow-radius<matrix.length) {
  109. for (int i = centerCol + radius - 1; i >= centerCol - radius; i--) {
  110. positions.add(new int[]{centerRow + radius, i});
  111. }
  112. }
  113. if (centerCol-radius>=0&&centerCol-radius<matrix[0].length) {
  114. for (int i = centerRow + radius - 1; i >= centerRow - radius + 1 ; i--) {
  115. positions.add(new int[]{i, centerCol - radius});
  116.  
  117. }
  118. }
  119.  
  120. ArrayList<int[]> validPositions = new ArrayList<>();
  121.  
  122. for (int[] position : positions) {
  123. if (position[0] >= 0 && position[0] < matrix.length){
  124. if (position[1] >=0 && position[1] < matrix[0].length){
  125. validPositions.add(position);
  126. }
  127. }
  128. }
  129.  
  130. return validPositions;
  131.  
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement