将矩阵旋转 180 度的 C++ 程序
给定一个方阵,任务是我们在不使用任何额外空间的情况下将其逆时针方向旋转 180 度。
例子 :
Input : 1 2 3
4 5 6
7 8 9
Output : 9 8 7
6 5 4
3 2 1
Input : 1 2 3 4
5 6 7 8
9 0 1 2
3 4 5 6
Output : 6 5 4 3
2 1 0 9
8 7 6 5
4 3 2 1
方法:1(仅打印旋转矩阵)
这个问题的解决方案是将矩阵旋转 180 度,我们可以很容易地遵循该步骤
Matrix = a00 a01 a02
a10 a11 a12
a20 a21 a22
when we rotate it by 90 degree
then matrix is
Matrix = a02 a12 a22
a01 a11 a21
a00 a10 a20
when we rotate it by again 90
degree then matrix is
Matrix = a22 a21 a20
a12 a11 a10
a02 a01 a00
从上图中,我们可以简单地将矩阵旋转 180 度,然后我们将不得不以相反的方式打印给定的矩阵。
C++
// C++ program to rotate a matrix by 180 degrees
#include
#define N 3
using namespace std;
// Function to Rotate the matrix by 180 degree
void rotateMatrix(int mat[][N])
{
// Simply print from last cell to first cell.
for (int i = N - 1; i >= 0; i--) {
for (int j = N - 1; j >= 0; j--)
printf("%d ", mat[i][j]);
printf("
");
}
}
// Driven code
int main()
{
int mat[N][N] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
rotateMatrix(mat);
return 0;
}
C++
// C++ program for left rotation of matrix by 180
#include
using namespace std;
#define R 4
#define C 4
// Function to rotate the matrix by 180 degree
void reverseColumns(int arr[R][C])
{
for (int i = 0; i < C; i++)
for (int j = 0, k = C - 1; j < k; j++, k--)
swap(arr[j][i], arr[k][i]);
}
// Function for transpose of matrix
void transpose(int arr[R][C])
{
for (int i = 0; i < R; i++)
for (int j = i; j < C; j++)
swap(arr[i][j], arr[j][i]);
}
// Function for display the matrix
void printMatrix(int arr[R][C])
{
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++)
cout << arr[i][j] << " ";
cout << '
';
}
}
// Function to anticlockwise rotate matrix
// by 180 degree
void rotate180(int arr[R][C])
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
// Driven code
int main()
{
int arr[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate180(arr);
printMatrix(arr);
return 0;
}
C++
#include
using namespace std;
/**
* Reverse Row at specified index in the matrix
* @param data matrix
* @param index row index
*/
void reverseRow(vector>& data,
int index)
{
int cols = data[index].size();
for(int i = 0; i < cols / 2; i++)
{
int temp = data[index][i];
data[index][i] = data[index][cols - i - 1];
data[index][cols - i - 1] = temp;
}
}
/**
* Print Matrix data
* @param data matrix
*/
void printMatrix(vector>& data)
{
for(int i = 0; i < data.size(); i++)
{
for(int j = 0; j < data[i].size(); j++)
{
cout << data[i][j] << " ";
}
cout << endl;
}
}
/**
* Rotate Matrix by 180 degrees
* @param data matrix
*/
void rotateMatrix180(vector>& data)
{
int rows = data.size();
int cols = data[0].size();
if (rows % 2 != 0)
{
// If N is odd reverse the middle
// row in the matrix
reverseRow(data, data.size() / 2);
}
// Swap the value of matrix [i][j] with
// [rows - i - 1][cols - j - 1] for half
// the rows size.
for(int i = 0; i <= (rows/2) - 1; i++)
{
for(int j = 0; j < cols; j++)
{
int temp = data[i][j];
data[i][j] = data[rows - i - 1][cols - j - 1];
data[rows - i - 1][cols - j - 1] = temp;
}
}
}
// Driver code
int main()
{
vector> data{ { 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 } };
// Rotate Matrix
rotateMatrix180(data);
// Print Matrix
printMatrix(data);
return 0;
}
// This code is contributed by divyeshrabadiya07
输出 :
9 8 7
6 5 4
3 2 1
时间复杂度: O(N*N)
辅助空间: O(1)
方法:2(就地旋转)
有四个步骤:
1-查找矩阵的转置。
2-反转转置的列。
3-找到矩阵的转置。
4- 转置的反转列
Let the given matrix be
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
First we find transpose.
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Then we reverse elements of every column.
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
then transpose again
4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13
Then we reverse elements of every column again
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
C++
// C++ program for left rotation of matrix by 180
#include
using namespace std;
#define R 4
#define C 4
// Function to rotate the matrix by 180 degree
void reverseColumns(int arr[R][C])
{
for (int i = 0; i < C; i++)
for (int j = 0, k = C - 1; j < k; j++, k--)
swap(arr[j][i], arr[k][i]);
}
// Function for transpose of matrix
void transpose(int arr[R][C])
{
for (int i = 0; i < R; i++)
for (int j = i; j < C; j++)
swap(arr[i][j], arr[j][i]);
}
// Function for display the matrix
void printMatrix(int arr[R][C])
{
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++)
cout << arr[i][j] << " ";
cout << '
';
}
}
// Function to anticlockwise rotate matrix
// by 180 degree
void rotate180(int arr[R][C])
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
// Driven code
int main()
{
int arr[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate180(arr);
printMatrix(arr);
return 0;
}
输出 :
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
时间复杂度: O(R*C)
辅助空间: O(1)
在上面的代码中,矩阵的转置必须被找到两次,并且列必须被反转两次。
所以,我们可以有更好的解决方案。
方法:3(位置交换)
在这里,我们交换各个位置的值。
C++
#include
using namespace std;
/**
* Reverse Row at specified index in the matrix
* @param data matrix
* @param index row index
*/
void reverseRow(vector>& data,
int index)
{
int cols = data[index].size();
for(int i = 0; i < cols / 2; i++)
{
int temp = data[index][i];
data[index][i] = data[index][cols - i - 1];
data[index][cols - i - 1] = temp;
}
}
/**
* Print Matrix data
* @param data matrix
*/
void printMatrix(vector>& data)
{
for(int i = 0; i < data.size(); i++)
{
for(int j = 0; j < data[i].size(); j++)
{
cout << data[i][j] << " ";
}
cout << endl;
}
}
/**
* Rotate Matrix by 180 degrees
* @param data matrix
*/
void rotateMatrix180(vector>& data)
{
int rows = data.size();
int cols = data[0].size();
if (rows % 2 != 0)
{
// If N is odd reverse the middle
// row in the matrix
reverseRow(data, data.size() / 2);
}
// Swap the value of matrix [i][j] with
// [rows - i - 1][cols - j - 1] for half
// the rows size.
for(int i = 0; i <= (rows/2) - 1; i++)
{
for(int j = 0; j < cols; j++)
{
int temp = data[i][j];
data[i][j] = data[rows - i - 1][cols - j - 1];
data[rows - i - 1][cols - j - 1] = temp;
}
}
}
// Driver code
int main()
{
vector> data{ { 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 } };
// Rotate Matrix
rotateMatrix180(data);
// Print Matrix
printMatrix(data);
return 0;
}
// This code is contributed by divyeshrabadiya07
输出 :
25 24 23 22 21
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1
时间复杂度: O(R*C)
辅助空间: O(1)
有关详细信息,请参阅有关将矩阵旋转 180 度的完整文章!