Join Regular Classroom : Visit ClassroomTech

Matrix Based Questions | Matrix Rotation | Codewindow.in

You are given a square matrix of dimension N. Let this matrix be called A. Your task is to rotate A in clockwise direction by S degrees, where S is angle of rotation. On the matrix, there will be 3 types of operations viz.
1. Rotation
Rotate the matrix A by angle S, presented as input in form of A S
2. Querying
Query the element at row K and column L, presented as input in form of Q K L
3. Updation
Update the element at row X and column Y with value Z, presented as input in form of U X Y Z. Print the output of individual operations as depicted in Output Specification. 

Input Format:
Input will consist of three parts, viz.
1. Size of the matrix (N)
2. The matrix itself (A = N * N)
3. Various operations on the matrix, one operation on each line. (Beginning either with A, Q or U)
-1 will represent end of input.

Note: Angle of rotation will always be multiples of 90 degrees only. All Update operations happen only on the initial matrix. After update all the previous rotations have to be applied on the updated matrix.

Output Format:
For each Query operation print the element present at K-L location of the matrix in its current state.

Constraints:
1<=N<=1000
1<=Aij<=1000
0<=S<=160000
1<=K, L<=N
1<=Q<=100000

Input:
2
1 2
3 4
A 90
Q 1 1
Q 1 2
A 90
Q 1 1
U 1 1 6
Q 2 2
-1

Output:
3
1
4
6

Explanation:
Initial Matrix
1 2
3 4
After 90 degree rotation, the matrix will become
3 1
4 2
Now the element at A11 is 3 and A12 is 1. Again the angle of rotation is 90 degree, now after the rotation the matrix will become
4 3
2 1
Now the element at A11 is 4. As the next operation is Update, update initial matrix i.e.
6 2
3 4
After updating, apply all the previous rotations (i.e. 180 = two 90 degree rotations).
The matrix will now become
4 3
2 6
Now A22 is 6.

Solution: In C

#include<stdio.h>

int main() {
    int n;
    scanf("%d",&n);
    int a[n][n],i,j,s=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    }
    while(1)
    {
        char c;
        scanf(" %c",&c);
        if(c == '-')
            return 0;
        else if(c=='A')
        {
            int x;
            scanf("%d",&x);
            s=s+x/90;
            s=s%4;
        }
        else if(c == 'Q')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x--;
            y--;
            if(s == 0) printf("%d\n",a[x][y]);
            if(s== 1) printf("%d\n",a[n-y-1][x]);
            if(s== 2) printf("%d\n",a[n-x-1][n-y-1]);
            if(s== 3) printf("%d\n",a[y][n-x-1]);
        }
        else if(c == 'U')
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            a[x-1][y-1]=z;
        }
    }
    return 0;
}

Solution: In Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bufferedReader.readLine());
        int[][] A = new int[N][N];
        for (int i = 0; i < N; i++) {
            String[] inputs = bufferedReader.readLine().split(" ");
            for (int j = 0; j < N; j++) {
                A[i][j] = Integer.parseInt(inputs[j]);
            }
        }
        int angle = 0;
        int[][] RA = new int[N][N];
        copy(A, RA, N);
        String line;
        while (!(line = bufferedReader.readLine()).equals("-1")) {
            if (line.startsWith("A")) {
                int S = Integer.parseInt(line.split(" ")[1]);
                angle += S;
                rotate(RA, N, S / 90);
            } else if (line.startsWith("Q")) {
                String[] inputs = line.split(" ");
                int K = Integer.parseInt(inputs[1]);
                int L = Integer.parseInt(inputs[2]);
                System.out.println(RA[K - 1][L - 1]);
            } else if (line.startsWith("U")) {
                String[] inputs = line.split(" ");
                int X = Integer.parseInt(inputs[1]),
                Y = Integer.parseInt(inputs[2]),
                Z = Integer.parseInt(inputs[3]);
                A[X - 1][Y - 1] = Z;
                RA = new int[N][N];
                copy(A, RA, N);
                rotate(RA, N, angle / 90);
            }
        }
    }
    
    private static void copy(int[][] A, int[][] RA, int N) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                RA[i][j] = A[i][j];
            }
        }
    }
    
    private static void rotate(int[][] RA, int N) {
        int[][] temp = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                temp[i][j] = RA[N - j - 1][i];
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                RA[i][j] = temp[i][j];
            }
        }
    }
    
    private static void rotate(int[][] RA, int N, int rotations) {
        for (int i = 1; i <= rotations; i++) {
            rotate(RA, N);
        }
    }
}

Solution: In Python 3

def rotate90Clockwise(A):
    N = len(A[0])
    for i in range(N // 2):
        for j in range(i, N - i - 1):
            temp = A[i][j]
            A[i][j] = A[N - 1 - j][i]
            A[N - 1 - j][i] = A[N - 1 - i][N - 1 - j]
            A[N - 1 - i][N - 1 - j] = A[j][N - 1 - i]
            A[j][N - 1 - i] = temp
    return A
    
n = int(input())
original_mat = []
for i in range(n):
    l = list(map(int,input().strip().split()))
    original_mat.append(l)
    copy_mat = list(map(list, original_mat))
angle_count = 0
while 1:
    query = input().split()
    if query[0] == 'A':
        count = int(query[1])
        count = count // 90
        count = count % 4
        angle_count += count
        for _ in range(count):
            original_mat = rotate90Clockwise(original_mat)
    elif query[0] == 'Q':
        r , c = int(query[1]) , int(query[2])
        print(original_mat[r-1][c-1])
    elif query[0] == 'U':
        r , c ,val = int(query[1]) , int(query[2]) , int(query[3])
        duplicate_matrix = list(map(list,copy_mat))
        duplicate_matrix[r-1][c-1] = val
        for _ in range(angle_count % 4):
            original_mat = rotate90Clockwise(duplicate_matrix)
    elif query[0] == '-1':
        exit()

Also Checkout

Recent Posts
Categories
Pages