Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class DragonTrap {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- char[][] matrix = new char[n][];
- char[][] model = new char[n][];
- for (int i = 0; i < n; i++) {
- char[] chars = scanner.nextLine().trim().toCharArray();
- matrix[i] = chars;
- model[i] = chars.clone();
- }
- while (true){
- String currentLine = scanner.nextLine().trim();
- if (currentLine.equals("End")){
- break;
- }
- currentLine = currentLine.replaceAll("[()]+", " ");
- String[] splitet = currentLine.split("\\s+");
- //String coordinates = splitet[0].substring(0, splitet[0].length());
- //String[] splitetCoordinates = coordinates.trim().split("\\s+");
- int row = Integer.parseInt(splitet[1]);
- int col = Integer.parseInt(splitet[2]);
- Integer radius = Integer.parseInt(splitet[3]);
- Integer numberOfRotations = Integer.parseInt(splitet[4]);
- if (numberOfRotations > 0){
- RotateClockwise(matrix, row, col, radius, numberOfRotations);
- } else if (numberOfRotations < 0) {
- RotateOpositeClockwise(matrix, row, col, radius, numberOfRotations);
- }
- }
- int changes = 0;
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j]);
- if (matrix[i][j]!=model[i][j]){
- changes++;
- }
- }
- System.out.println();
- }
- System.out.println("Symbols changed: " + changes);
- }
- private static void RotateOpositeClockwise(char[][] matrix, int centerRow, int centerCol, Integer radius, Integer numberOfRotations) {
- ArrayList<int[]> positionsToRotate = findValidPositions(matrix, centerRow, centerCol, radius);
- if (positionsToRotate.size()==0){
- return ;
- }
- for (int j = 0; j < numberOfRotations%positionsToRotate.size(); j++) {
- Character prev = matrix[positionsToRotate.get(positionsToRotate.size()-1)[0]][positionsToRotate.get(positionsToRotate.size()-1)[1]];
- for (int i = positionsToRotate.size()-2; i >= 0 ; i++) {
- Character current = matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]];
- matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]] = prev;
- prev = current;
- }
- matrix[positionsToRotate.get(positionsToRotate.size()-1)[0]][positionsToRotate.get(positionsToRotate.size()-1)[1]] = prev;
- }
- }
- private static void RotateClockwise(char[][] matrix, int centerRow, int centerCol, Integer radius, Integer numberOfRotations) {
- ArrayList<int[]> positionsToRotate = findValidPositions(matrix, centerRow, centerCol, radius);
- if (positionsToRotate.size()==0){
- return ;
- }
- for (int j = 0; j < numberOfRotations%positionsToRotate.size(); j++) {
- Character prev = matrix[positionsToRotate.get(0)[0]][positionsToRotate.get(0)[1]];
- for (int i = 1; i < positionsToRotate.size(); i++) {
- Character current = matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]];
- matrix[positionsToRotate.get(i)[0]][positionsToRotate.get(i)[1]] = prev;
- prev = current;
- }
- matrix[positionsToRotate.get(0)[0]][positionsToRotate.get(0)[1]] = prev;
- }
- }
- private static ArrayList<int[]> findValidPositions(char[][] matrix, int centerRow, int centerCol, Integer radius) {
- ArrayList<int[]> positions = new ArrayList<>();
- if (centerRow-radius >= 0&¢erRow-radius<matrix.length) {
- for (int i = centerCol - radius; i <= centerCol + radius; i++) {
- positions.add(new int[]{centerRow - radius, i});
- }
- }
- if (centerCol+radius>=0&¢erCol<matrix[0].length) {
- for (int i = centerRow - radius + 1; i <= centerRow + radius; i++) {
- positions.add(new int[]{i, centerCol + radius});
- }
- }
- if (centerRow+radius >= 0&¢erRow-radius<matrix.length) {
- for (int i = centerCol + radius - 1; i >= centerCol - radius; i--) {
- positions.add(new int[]{centerRow + radius, i});
- }
- }
- if (centerCol-radius>=0&¢erCol-radius<matrix[0].length) {
- for (int i = centerRow + radius - 1; i >= centerRow - radius + 1 ; i--) {
- positions.add(new int[]{i, centerCol - radius});
- }
- }
- ArrayList<int[]> validPositions = new ArrayList<>();
- for (int[] position : positions) {
- if (position[0] >= 0 && position[0] < matrix.length){
- if (position[1] >=0 && position[1] < matrix[0].length){
- validPositions.add(position);
- }
- }
- }
- return validPositions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement