Jun 27, 2016

Program to rotate a 2D matrix

Problem
A 2D matrix, A, of dimension m x n. You have to rotate the matrix 1 time in the clockwise direction and print the resultant matrix. 
Input Format
First Line:  <m>space<n>
Subsequent lines takes Matrix as input.
Example:
Input
3 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Here 3 4 in first line are m(number of rows), and n (number of columns)
Subsequent lines are the Matrix itself.

Expected Output:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
Program:
Our matrix rotation logic does the following:

  1. Rotate elements of first row
  2. Rotate elements of last column
  3. Rotate elements of last row from the remaining rows
  4. Rotate elements of first column from the remaining rows
Source Code:

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class MatrixRotation {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  try {
   // Input number of rows, columns and matrix elements
   int numberOfRows = in.nextInt();
   int numberOfColumns = in.nextInt();
   int matrix[][] = new int[numberOfRows][numberOfColumns];
   for (int i = 0; i < numberOfRows; i++) {
    for (int j = 0; j < numberOfColumns; j++) {
     matrix[i][j] = in.nextInt();
    }
   }

   rotateMatrix(matrix, numberOfRows, numberOfColumns);
   
   // Print rotated matrix
   for (int i = 0; i < numberOfRows; i++) {
    for (int j = 0; j < numberOfColumns; j++) {
     System.out.print(matrix[i][j] + "  ");
    }
    System.out.println();
   }
  } catch (InputMismatchException exception) {
   System.out.println("Invalid input. Only numbers are allowed");
  } finally {
   in.close();
  }
 }

 /**
  * A function to rotate a matrix mat[][] of size M X N.
  * 
  * @param mat matrix of size m x n
  * @param rowSize number of rows in matrix, m
  * @param colSize number of columns in matrix, n
  */
 public static void rotateMatrix(int mat[][], int rowSize, int colSize) {

  // pointers for current row and column
  int currRow = 0;
  int currColumn = 0;
  int prev, curr;

  // p and q defines the number of rows and columns that are not rotated, keeps on decrementing with every iteration
  int p = rowSize;
  int q = colSize;

  while (currRow < p && currColumn < q) {

   if (currRow + 1 == p || currColumn + 1 == q)
    break;

   // store next row's first element, will be copied at first position of current row
   prev = mat[currRow + 1][currColumn];

   // Move elements of first row from the remaining rows
   for (int i = currColumn; i < q; i++) {
    curr = mat[currRow][i];
    mat[currRow][i] = prev;
    prev = curr;
   }
   currRow++;

   // Move elements of last column from the remaining columns
   for (int i = currRow; i < p; i++) {
    curr = mat[i][q - 1];
    mat[i][q - 1] = prev;
    prev = curr;
   }
   q--;

   // Move elements of last row from the remaining rows
   if (currRow < p) {
    for (int i = q - 1; i >= currColumn; i--) {
     curr = mat[p - 1][i];
     mat[p - 1][i] = prev;
     prev = curr;
    }
   }
   p--;

   // Move elements of first column from the remaining rows
   if (currColumn < q) {
    for (int i = p - 1; i >= currRow; i--) {
     curr = mat[i][currColumn];
     mat[i][currColumn] = prev;
     prev = curr;
    }
   }
   currColumn++;
  }

 }

Hopefully you liked the article. Please post your queries in the comments sections. 

0 comments:

Post a Comment