#33408: 解答

sivs911303@gmail.com (尛)

School : 國立彰化師範大學附屬高級工業職業學校
ID : 129662
IP address : [114.33.95.172]
Last Login ：
2023-01-07 20:28:05
b266. 矩陣翻轉 -- | From: [163.23.180.250] | Post Date : 2023-01-04 14:25

#include <iostream>

using namespace std;

int **matrix;
int r, c;

void output()
{
cout << r << " " << c << endl;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
cout << matrix[i][j];
if(j + 1 != c) cout << " ";
}
cout << endl;
}
}

void my_swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}

void flip()
{
for(int i = 0; i < r / 2; i++)
for(int j = 0; j < c; j++)
my_swap(matrix[i][j], matrix[r - i - 1][j]);
}

void spin()
{
my_swap(r, c);

int **newMatrix = new int*[r];
for(int i = 0; i < r; i++)
{
newMatrix[i] = new int[c];
for(int j = 0; j < c; j++)
newMatrix[i][j] = matrix[j][r - 1 - i];
}

matrix = newMatrix;
}

int main()
{
int m;
cin >> r >> c >> m;

matrix = new int*[r];
for(int i = 0; i < r; i++)
{
matrix[i] = new int[c];
for(int j = 0; j < c; j++)
cin >> matrix[i][j];
}

int order[m];
for(int i = m - 1; i >= 0; i--)
cin >> order[i];

for(int i = 0; i < m; i++)
if(order[i] == 0) spin();
else flip();

output();

return 0;
}

ZeroJudge Forum