Created
December 12, 2015 17:51
-
-
Save 1kohei1/396b2d7450bab54558e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Playground { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int row = scanner.nextInt(); | |
int col = scanner.nextInt(); | |
int numRotations = scanner.nextInt(); | |
int[][] map = new int[row][col]; | |
for (int i = 0; i < row; i++) { | |
for (int j = 0; j < col; j++) { | |
map[i][j] = scanner.nextInt(); | |
} | |
} | |
for (int i = 0; i < numRotations; i++) { | |
map = rotate(map, row, col); | |
} | |
printMap(map, row, col); | |
} | |
public static int[][] rotate(int[][] map, int row, int col) { | |
int[][] newMap = new int[row][col]; | |
int rowStart = 0; | |
int colStart = 0; | |
while (rowStart * 2 < row && colStart * 2 < col) { | |
// Top | |
for (int i = colStart +1; i < col - colStart; i++) { | |
newMap[rowStart][i - 1] = map[rowStart][i]; | |
} | |
// Right | |
for (int i = rowStart + 1; i < row - rowStart; i++) { | |
newMap[i - 1][col - colStart - 1] = map[i][col - colStart - 1]; | |
} | |
// Bottom | |
for (int i = colStart; i < col - colStart - 1; i++) { | |
newMap[row- rowStart - 1][i + 1] = map[row - rowStart - 1][i]; | |
} | |
// Left | |
for (int i = rowStart; i < row - rowStart - 1; i++) { | |
newMap[i + 1][colStart] = map[i][colStart]; | |
} | |
rowStart++; | |
colStart++; | |
} | |
return newMap; | |
} | |
public static void printMap(int[][] map, int row, int col) { | |
for (int i = 0; i < row; i++) { | |
for (int j = 0; j < col; j++) { | |
System.out.printf("%d ", map[i][j]); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment