传送门:http://poj.org/problem?id=3106
Description
Let us define a set of operations on a rectangular matrix of printable characters.
A matrix A with m rows (1-st index) and n columns (2-nd index) is given. The resulting matrix B is defined as follows.
- Transposition by the main diagonal (operation identifier is ‘
1
’): Bj,i = Ai,j - Transposition by the second diagonal (‘
2
’): Bn−j+1,m−i+1 = Ai,j - Horizontal flip (‘
H
’): Bm−i+1,j = Ai,j - Vertical flip (‘
V
’): Bi,n−j+1 = Ai,j - Rotation by 90 (‘
A
’), 180 (‘B
’), or 270 (‘C
’) degrees clockwise; 90 degrees case: Bj,m−i+1 = Ai,j - Rotation by 90 (‘
X
’), 180 (‘Y
’), or 270 (‘Z
’) degrees counterclockwise; 90 degrees case: Bn−j+1,i = Ai,j
You are given a sequence of no more than 100 000 operations from the set. Apply the operations to the given matrix and output the resulting matrix.
Input
At the first line of the input file there are two integer numbers — m and n (0 < m, n ≤ 300). Then there are m lines with n printable characters per line (we define a printable character as a symbol with ASCII code from 33 to 126 inclusive). There will be no additional symbols at these lines.
The next line contains the sequence operations to be performed, specified by their one-character identifiers. The operations should be performed from left to right.
Output
Two integer numbers, the number of rows and columns in the output matrix. Then the output matrix must follow, in the same format as the input one.
Sample Input
3 4 0000 a0b0 cdef A1
Sample Output
3 4 cdef a0b0 0000
原来TLT 优化后91ms
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 1000003
int a[maxn], q[maxn];
inline int read(){ int res(0),sign(1); char c;
while(1){ c = getchar(); if('0' <= c && c <= '9') { res = c - '0'; break; } else if(c == '-') { sign = -1; break; } }
while(1){ c = getchar(); if('0' <= c && c <= '9') res = res*10 + c - '0'; else break; }
return res * sign;
}
inline void put(int x){
if(x< 0){
putchar('-');
x = -x;
}
if(x == 0){
putchar('0');
return;
}
char s[20];
int bas = 0;
for(;x;x/=10)s[bas++] = x%10+'0';
for(;bas--;)putchar(s[bas]);
return;
}
#define MAXN 100005
#define For(i,m,n) for(i=m;i<n;i++)
#define MOD 1000000007
int r[2][2]={1,2,3,4};
void Swap(int *x, int *y)
{
int t;
t=*x; *x=*y; *y=t;
}
void op1()
{
Swap(&r[0][1],&r[1][0]);
}
void op2()
{
Swap(&r[0][0],&r[1][1]);
}
void opH()
{
Swap(&r[0][0],&r[1][0]);
Swap(&r[0][1],&r[1][1]);
}
main()
{
int i, j, m, n, flag, ti, tj, cnt=0;
char ch, Map[330][330];
m=read(),n=read();
//getchar();
For(i,0,m) gets(Map[i]);//printf("%s\n",Map[i]);
while(ch = getchar(),ch!='\n'){
//printf("%c\n",ch);
if(ch=='1') op1(),cnt++;
if(ch=='2') op2(),cnt++;
if(ch=='H') opH();
if(ch=='V') op1(),op2(),opH();
if(ch=='A'||ch=='Z') op2(),opH(),cnt++;
if(ch=='B'||ch=='Y') op1(),op2();
if(ch=='C'||ch=='X') op1(),opH(),cnt++;
}
if(cnt%2) Swap(&m,&n);
if(r[0][0]==1&&r[0][1]==2&&r[1][0]==3) flag=1;
if(r[0][0]==1&&r[0][1]==3&&r[1][0]==2) flag=2;
if(r[0][0]==2&&r[0][1]==1&&r[1][0]==4) flag=3;
if(r[0][0]==2&&r[0][1]==4&&r[1][0]==1) flag=4;
if(r[0][0]==3&&r[0][1]==1&&r[1][0]==4) flag=5;
if(r[0][0]==3&&r[0][1]==4&&r[1][0]==1) flag=6;
if(r[0][0]==4&&r[0][1]==2&&r[1][0]==3) flag=7;
if(r[0][0]==4&&r[0][1]==3&&r[1][0]==2) flag=8;
//printf("%d %d\n",m,n);
put(m),putchar(' '),put(n),putchar('\n');
For(i,0,m){
For(j,0,n){
if(flag==1) ti=i, tj=j;
if(flag==2) ti=j, tj=i;
if(flag==3) ti=i, tj=n-j-1;
if(flag==4) ti=j, tj=m-i-1;
if(flag==5) ti=n-j-1, tj=i;
if(flag==6) ti=m-i-1, tj=j;
if(flag==7) ti=n-j-1, tj=m-i-1;
if(flag==8) ti=m-i-1, tj=n-j-1;
putchar(Map[ti][tj]);
}
putchar('\n');
}
return 0;
}