Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- import java.util.ArrayList;
- 2
- import java.util.List;
- 3
- import java.util.Scanner;
- 4
- 5
- public class _20151004_DragonTrap {
- 6
- 7
- private static List<Point> coordinates = new ArrayList<>();
- 8
- private static List<Character> charsToRotate = new ArrayList<>();
- 9
- private static char[][] originalMatrix;
- 10
- private static char[][] matrix;
- 11
- 12
- 13
- public static void main(String[] args) {
- 14
- Scanner scanner = new Scanner(System.in);
- 15
- 16
- int numberOfRows = Integer.parseInt(scanner.nextLine());
- 17
- matrix = new char[numberOfRows][numberOfRows];
- 18
- originalMatrix = new char[numberOfRows][numberOfRows];
- 19
- 20
- fillMatrix(numberOfRows, scanner);
- 21
- 22
- String inputLine = scanner.nextLine();
- 23
- while (true) {
- 24
- if (inputLine.equals("End")) {
- 25
- break;
- 26
- }
- 27
- String[] splitedLine = inputLine.replaceAll("[()]", "").split(" ");
- 28
- int row = Integer.parseInt(splitedLine[0]);
- 29
- int col = Integer.parseInt(splitedLine[1]);
- 30
- int radius = Integer.parseInt(splitedLine[2]);
- 31
- int rotation = Integer.parseInt(splitedLine[3]);
- 32
- 33
- getChars(row, col, radius, rotation);
- 34
- rotateCollectedChars(rotation);
- 35
- collectRotatedChars();
- 36
- 37
- inputLine = scanner.nextLine();
- 38
- }
- 39
- int chars = countDifferentChars();
- 40
- printMatrix(chars);
- 41
- }
- 42
- 43
- private static void printMatrix(int chars) {
- 44
- for (int i = 0; i < matrix.length; i++) {
- 45
- System.out.println(matrix[i]);
- 46
- }
- 47
- System.out.printf("Symbols changed: %d",chars);
- 48
- }
- 49
- 50
- private static int countDifferentChars() {
- 51
- int countDiffChars = 0;
- 52
- for (int i = 0; i < originalMatrix.length; i++) {
- 53
- for (int j = 0; j < originalMatrix[i].length; j++) {
- 54
- if (matrix[i][j] != originalMatrix[i][j]) {
- 55
- countDiffChars++;
- 56
- }
- 57
- }
- 58
- }
- 59
- return countDiffChars;
- 60
- }
- 61
- 62
- private static void collectRotatedChars() {
- 63
- int index = 0;
- 64
- for (Point point : coordinates) {
- 65
- matrix[point.getX()][point.getY()] = charsToRotate.get(index);
- 66
- index++;
- 67
- }
- 68
- charsToRotate.clear();
- 69
- coordinates.clear();
- 70
- }
- 71
- 72
- private static void rotateCollectedChars(int rotation) {
- 73
- if (charsToRotate.size() == 0) {
- 74
- return;
- 75
- }
- 76
- int timesToRotate = Math.abs(rotation) % charsToRotate.size();
- 77
- if (rotation >= 0) {
- 78
- for (int i = 0; i < timesToRotate; i++) {
- 79
- char last = charsToRotate.get(charsToRotate.size() - 1);
- 80
- charsToRotate.remove(charsToRotate.size() - 1);
- 81
- charsToRotate.add(0, last);
- 82
- }
- 83
- }else{
- 84
- for (int i = 0; i < timesToRotate; i++) {
- 85
- char first = charsToRotate.get(0);
- 86
- charsToRotate.remove(0);
- 87
- charsToRotate.add(first);
- 88
- }
- 89
- }
- 90
- }
- 91
- 92
- private static void getChars(int row, int col, int radius, int rotation) {
- 93
- int startRow = row - radius;
- 94
- int endRow = row + radius;
- 95
- int startCol = col - radius;
- 96
- int endCol = col + radius;
- 97
- 98
- for (int i = startCol; i <= endCol; i++) {
- 99
- Point point = new Point(startRow, i);
- 100
- if (isInsideTheMatrix(point)) {
- 101
- coordinates.add(point);
- 102
- charsToRotate.add(matrix[point.getX()][point.getY()]);
- 103
- 104
- }
- 105
- }
- 106
- 107
- for (int i = startRow+1; i < endRow; i++) {
- 108
- Point point = new Point(i, endCol);
- 109
- if (isInsideTheMatrix(point)) {
- 110
- coordinates.add(point);
- 111
- charsToRotate.add(matrix[point.getX()][point.getY()]);
- 112
- 113
- }
- 114
- }
- 115
- 116
- for (int i = endCol; i >= startCol; i--) {
- 117
- Point point = new Point(endRow, i);
- 118
- if (isInsideTheMatrix(point)) {
- 119
- coordinates.add(point);
- 120
- charsToRotate.add(matrix[point.getX()][point.getY()]);
- 121
- 122
- }
- 123
- }
- 124
- for (int i = endRow-1; i > startRow; i--) {
- 125
- Point point = new Point(i, startCol);
- 126
- if (isInsideTheMatrix(point)) {
- 127
- coordinates.add(point);
- 128
- charsToRotate.add(matrix[point.getX()][point.getY()]);
- 129
- 130
- }
- 131
- }
- 132
- }
- 133
- 134
- private static boolean isInsideTheMatrix(Point point) {
- 135
- return point.getX() < matrix.length
- 136
- && point.getX() >= 0
- 137
- && point.getY() < matrix[point.getX()].length
- 138
- && point.getY() >= 0;
- 139
- }
- 140
- 141
- private static void fillMatrix(int numberOfRows, Scanner scanner) {
- 142
- for (int i = 0; i < numberOfRows; i++) {
- 143
- String input = scanner.nextLine();
- 144
- originalMatrix[i] = input.toCharArray();
- 145
- matrix[i] = input.toCharArray();
- 146
- }
- 147
- }
- 148
- }
- 149
- 150
- class Point{
- 151
- 152
- private int x;
- 153
- private int y;
- 154
- 155
- public Point(int x, int y) {
- 156
- super();
- 157
- this.x = x;
- 158
- this.y = y;
- 159
- }
- 160
- 161
- public int getX() {
- 162
- return x;
- 163
- }
- 164
- 165
- public int getY() {
- 166
- return y;
- 167
- }
- 168
- 169
- @Override
- 170
- public String toString() {
- 171
- return "Point [x=" + x + ", y=" + y + "]";
- 172
- }
- 173
- 174
- }
Advertisement
Add Comment
Please, Sign In to add comment