蒜头君给定?mm?行?nn?列的图像各像素点灰度值,对其依次进行一系列操作后,求最终图像。
其中,可能的操作及对应字符有如下四种:
A:顺时针旋转?9090?度;
B:逆时针旋转?9090?度;
C:左右翻转;
D:上下翻转。
输入格式
第一行包含两个正整数?mm?和?nn,表示图像的行数和列数,中间用单个空格隔开。1 \le m \le 100, 1 \le n \le 1001≤m≤100,1≤n≤100。
接下来?mm?行,每行?nn?个整数,表示图像中每个像素点的灰度值,相邻两个数之间用单个空格隔开。灰度值范围在?00?到?255255?之间。
接下来一行,包含由?AA、BB、CC、DD?组成的字符串?ss,表示需要按顺序执行的操作序列。ss?的长度在?11?到?100100?之间。
输出格式
m'm′?行,每行包含?n'n′?个整数,为最终图像各像素点的灰度值。其中?m'm′?为最终图像的行数,n'n′?为最终图像的列数。相邻两个整数之间用单个空格隔开。
Sample 1
Inputcopy | Outputcopy |
---|
2 3
10 0 10
100 100 10
AC | 10 100
0 100
10 10 |
代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int m, n;
int a[105][105], res[105][105];
void A() {
m ^= n ^= m ^= n;
memset(res, 0, sizeof(res));
for (int i = 1, k = 1; i <= m, k <= m; i ++, k ++) {
for (int j = n, r = 1; j >= 1, r <= n; j --, r ++) {
res[k][r] = a[j][i];
}
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
a[i][j] = res[i][j];
}
}
}
void B() {
m ^= n ^= m ^= n;
memset(res, 0, sizeof(res));
for (int k = 1, i = m; k <= m, i >= 1; k ++, i --) {
for (int r = 1, j = 1; r <= n, j <= n; r ++, j ++) {
res[k][r] = a[j][i];
}
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
a[i][j] = res[i][j];
}
}
}
void C() {
memset(res, 0, sizeof(res));
for (int i = 1; i <= m; i ++) {
for (int j = n, k = 1; j >= 1, k <= n; j --, k ++) {
res[i][k] = a[i][j];
}
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
a[i][j] = res[i][j];
}
}
}
void D() {
memset(res, 0, sizeof(res));
for (int i = m, k = 1; i >= 1, k <= m; i --, k ++) {
for (int j = 1; j <= n; j ++) {
res[k][j] = a[i][j];
}
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
a[i][j] = res[i][j];
}
}
}
int main() {
cin >> m >> n;
for (int i = 1; i <= m; i ++)
for (int j = 1; j <= n; j ++)
cin >> a[i][j];
string s;
cin >> s;
for (int i = 0; i < s.size(); i ++) {
if (s[i] == 'A') A();
if (s[i] == 'B') B();
if (s[i] == 'C') C();
if (s[i] == 'D') D();
}
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
cout << res[i][j] << " ";
}
cout << "\n";
}
return 0;
}
|