Advertisement
scottashipp

Rotate a Matrix

Feb 10th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. public class MatrixUtilities {
  2.    
  3.     static int counter;
  4.     static final String TOTAL_ITERATIONS = "\nPerformed %d swaps.";
  5.     static final String SEPARATOR = "\n\n";
  6.    
  7.     public static void rotateCounter(int[][] a) {
  8.         counter = 0;
  9.         int len = a.length;
  10.         for(int i = 0; i < (len / 2); i++) {
  11.             for(int j = 0; j < len; j++) {
  12.                 swap(a, j, i, j, ((len-1) - i));
  13.                 counter++;
  14.             }
  15.         }
  16.        
  17.         for(int i = 0; i < len; i++) {
  18.             for(int j = i; j < len; j++) {
  19.                 if(i == j) {
  20.                     continue;
  21.                 }
  22.                 swap(a, i, j, j, i);
  23.                 counter++;
  24.             }
  25.         }
  26.     }
  27.    
  28.     public static void rotate(int[][] a) {
  29.         counter = 0;
  30.         int len = a.length;
  31.         for(int i = 0; i < (len / 2); i++) {
  32.             for(int j = 0; j < len; j++) {
  33.                 swap(a, i, j, ((len-1) - i), j);
  34.                 counter++;
  35.             }
  36.         }
  37.        
  38.         for(int i = 0; i < len; i++) {
  39.             for(int j = i; j < len; j++) {
  40.                 if(i == j) {
  41.                     continue;
  42.                 }
  43.                 swap(a, i, j, j, i);
  44.                 counter++;
  45.             }
  46.         }
  47.     }
  48.    
  49.     public static void swap(int[][] a, int i, int j, int x, int y) {
  50.         int temp = a[i][j];
  51.         a[i][j] = a[x][y];
  52.         a[x][y] = temp;
  53.     }
  54.    
  55.     public static void printOut(int[][] a) {
  56.         int len = a.length;
  57.         for(int i = 0; i < len; i++) {
  58.             for(int j = 0; j < len; j++) {
  59.                 System.out.print(a[i][j] + " ");
  60.             }
  61.             System.out.println();
  62.         }
  63.     }
  64.    
  65.     public static void printRotateAndPrint(int[][] a, Rotation r) {
  66.         printOut(a);
  67.         if(r == Rotation.CLOCKWISE){
  68.             rotate(a);
  69.         }
  70.         else {
  71.             rotateCounter(a);
  72.         }
  73.        
  74.         System.out.println("---ROTATED TO---");
  75.         printOut(a);
  76.         System.out.printf(TOTAL_ITERATIONS, counter);
  77.     }
  78.    
  79.     public static enum Rotation {
  80.         CLOCKWISE, COUNTER_CLOCKWISE;
  81.     }
  82.    
  83.     public static void main(String[] args) {
  84.         int[][] a = { {1,2}, {3,4}};
  85.         printRotateAndPrint(a, Rotation.CLOCKWISE);
  86.         System.out.println(SEPARATOR);
  87.         int[][] b = { {1,2,3}, {4,5,6}, {7,8,9}};
  88.         printRotateAndPrint(b, Rotation.CLOCKWISE);
  89.         System.out.println(SEPARATOR);
  90.         int[][] c = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} };
  91.         printRotateAndPrint(c, Rotation.CLOCKWISE);
  92.         System.out.println(SEPARATOR);
  93.         int[][] d = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19,20}, {21,22,23,24,25}};
  94.         printRotateAndPrint(d, Rotation.CLOCKWISE);
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement