इस लेख में, हम समझेंगे कि मैट्रिक्स एलिमेंट्स को कैसे रोटेट किया जाता है। मैट्रिक्स पंक्तियों और स्तंभों में एलिमेंट्स का प्रतिनिधित्व है। मैट्रिक्स रोटेशन मैट्रिक्स के प्रत्येक एलिमेंट की स्थिति को दाएं या बाएं 1 पोजीशन की ओर शिफ्ट करना है।
उदाहरण
मान लीजिए हमारा इनपुट है:
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
एल्गोरिदम
चरण 1 - शुरू करें चरण 2 - एक इंटीजर मैट्रिक्स घोषित करें: input_matrix और चार इंटीजर वैल्यू घोषित करें: row, column, previous, next चरण 3 - मान परिभाषित करें चरण 4 - व्हाइल लूप का उपयोग करके मैट्रिक्स के प्रत्येक एलिमेंट पर पुनरावृति करें और कई फॉर-लूप्स का उपयोग करके प्रत्येक एलिमेंट की स्थिति को दाएं एक पोजीशन शिफ्ट करें और मैट्रिक्स स्टोर करें चरण 5 - परिणाम प्रदर्शित करें चरण 6 - रुकें
उदाहरण 1: यूजर इनपुट आधारित
यहां, इनपुट यूजर द्वारा प्रॉम्प्ट के आधार पर दर्ज किया जाता है।
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("इनपुट मैट्रिक्स परिभाषित है: ");
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("\nएक रोटेशन के बाद इनपुट मैट्रिक्स: ");
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");
}
}
}
आउटपुट
इनपुट मैट्रिक्स परिभाषित है: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 एक रोटेशन के बाद इनपुट मैट्रिक्स: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12
उदाहरण 2: फंक्शन में एनकैप्सुलेटेड
यहां, इंटीजर पहले से परिभाषित है, और इसका मान एक्सेस करके कंसोल पर प्रदर्शित किया जाता है।
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("\nएक रोटेशन के बाद मैट्रिक्स: ");
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("मैट्रिक्स परिभाषित है: ");
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);
}
}
आउटपुट
मैट्रिक्स परिभाषित है: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 एक रोटेशन के बाद मैट्रिक्स: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12