http://poj.org/problem?id=3106
#include <stdio.h>
static char str[100010];
static char input[310][310], ans[310][310];
static int n, m;
static int doit( int s ) {
int i = s/1000, j = s%1000, a = i, b = j;
int nn = n;
int mm = m, t;
for( int k=0; str[k]; k++ ) {
switch( str[k] ){
case '1':
a = j; b = i;
t = nn; nn = mm; mm = t;
break;
case '2':
a = nn-j; b = mm-i;
t = nn; nn = mm; mm = t;
break;
case 'H':
a = mm-i; b = j; break;
case 'V':
a = i; b = nn-j; break;
case 'B': case 'Y':
a = mm-i; b = nn-j; break;
case 'A': case 'Z':
a = j; b = mm-i;
t = nn; nn = mm; mm = t;
break;
case 'C': case 'X':
a = nn-j; b = i;
t = nn; nn = mm; mm = t;
break;
}
i = a;
j = b;
}
return i*1000+j;
}
int main( ){
int i, j;
scanf( "%d%d", &m, &n );
for( i=0; i<m; i++ )
scanf( "%s", input[i] );
getchar( );
gets( str );
int *x=new int[3], *y = new int[3];
int t;
n--; m--;
t = doit( 0*1000+0 );
x[0] = t/1000; y[0] = t%1000;
t = doit( m*1000+0 );
x[1] = t/1000; y[1] = t%1000;
t = doit( 0*1000+n );
x[2] = t/1000; y[2] = t%1000;
n++;m++;
int a = x[0], b = y[0], aa, bb;
int da = ((x[1]==x[0]) ? 0 : ((x[1]>x[0])?1:-1) ),
db = ((y[1]==y[0]) ? 0 : ((y[1]>y[0])?1:-1) ),
da1= ((x[2]==x[0]) ? 0 : ((x[2]>x[0])?1:-1) ),
db1= ((y[2]==y[0]) ? 0 : ((y[2]>y[0])?1:-1) );
int nn = 0, mm = 0;
for( i=0; i<m; i++ ) {
aa = a; bb = b;
for( j=0; j<n; j++ ) {
ans[aa][bb] = input[i][j];
if( aa > nn )
nn = aa;
if( bb > mm )
mm = bb;
aa += da1; bb += db1;
}
a += da; b += db;
}
nn++;mm++;
printf( "%d %dn", nn, mm );
for( i=0; i<nn; i++ ) {
for( j=0; j<mm; j++ )
printf( "%c", ans[i][j] );
printf( "n" );
}
return 0;
}