এই নিবন্ধে আমরা বুঝব কিভাবে ম্যাট্রিক্স এলিমেন্ট রোটেট করা যায়। ম্যাট্রিক্স হল এলিমেন্টগুলোর সারি ও কলাম আকারে রিপ্রেজেন্টেশন। ম্যাট্রিক্স রোটেশন হল ম্যাট্রিক্সের প্রতিটি এলিমেন্টের পজিশন ১ পজিশন ডানে বা বামে শিফ্ট করা।
ডেমো
ইনপুট:
The matrix is defined as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
আউটপুট:
The matrix after one rotation: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12
এলগরিদম
ধাপ ১ - শুরু ধাপ ২ - একটি ইন্টিজার ম্যাট্রিক্স (input_matrix) এবং চারটি ইন্টিজার ভেরিয়েবল (row, column, previous, next) ডিক্লেয়ার করুন ধাপ ৩ - মানগুলো সংজ্ঞায়িত করুন ধাপ ৪ - হোল-লুপ ব্যবহার করে ম্যাট্রিক্সের প্রতিটি এলিমেন্ট পরিদর্শন করুন এবং একাধিক ফর-লুপ ব্যবহার করে প্রতিটি এলিমেন্টের পজিশন এক পজিশন ডানে শিফ্ট করুন এবং ম্যাট্রিক্সটি সংরক্ষণ করুন ধাপ ৫ - ফলাফল প্রদর্শন করুন ধাপ ৬ - থামুন
উদাহরণ ১: ইউজার ইনপুট নেওয়া
public class RotateMatrix {
static int Rows = 4;
static int Columns = 4;
public static void main(String[] args) {
int input_matrix[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
System.out.println("The input_matrix is defined as ");
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
int m = Rows, n = Columns;
int row = 0, column = 0;
int previous, current;
while (row < m && column < n) {
if (row + 1 == m || column + 1 == n)
break;
previous = input_matrix[row + 1][column];
for (int i = column; i < n; i++) {
current = input_matrix[row][i];
input_matrix[row][i] = previous;
previous = current;
}
row++;
for (int i = row; i < m; i++) {
current = input_matrix[i][n-1];
input_matrix[i][n-1] = previous;
previous = current;
}
n--;
if (row < m) {
for (int i = n-1; i >= column; i--) {
current = input_matrix[m-1][i];
input_matrix[m-1][i] = previous;
previous = current;
}
}
m--;
if (column < n) {
for (int i = m-1; i >= row; i--) {
current = input_matrix[i][column];
input_matrix[i][column] = previous;
previous = current;
}
}
column++;
}
System.out.println("\nThe input_matrix after one rotation: ");
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
}
}
আউটপুট
The input_matrix is defined as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The input_matrix after one rotation: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12
উদাহরণ ২: অবজেক্ট-ওরিয়েন্টেড পদ্ধতিতে ফাংশন ব্যবহার
public class RotateMatrix {
static int Rows = 4;
static int Columns = 4;
static void Rotate_matrix(int m, int n, int matrix[][]) {
int row = 0, column = 0;
int previous, current;
while (row < m && column < n) {
if (row + 1 == m || column + 1 == n)
break;
previous = matrix[row + 1][column];
for (int i = column; i < n; i++) {
current = matrix[row][i];
matrix[row][i] = previous;
previous = current;
}
row++;
for (int i = row; i < m; i++) {
current = matrix[i][n-1];
matrix[i][n-1] = previous;
previous = current;
}
n--;
if (row < m) {
for (int i = n-1; i >= column; i--) {
current = matrix[m-1][i];
matrix[m-1][i] = previous;
previous = current;
}
}
m--;
if (column < n) {
for (int i = m-1; i >= row; i--) {
current = matrix[i][column];
matrix[i][column] = previous;
previous = current;
}
}
column++;
}
System.out.println("\nThe matrix after one rotation: ");
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++)
System.out.print( matrix[i][j] + " ");
System.out.print("\n");
}
}
public static void main(String[] args) {
int input_matrix[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
System.out.println("The matrix is defined as ");
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
Rotate_matrix(Rows, Columns, input_matrix);
}
}
আউটপুট
The matrix is defined as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The matrix after one rotation: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12