题目链接:http://codeforces.com/problemset/problem/400/C
题目意思:给出一个n行m列的矩阵,问经过 x 次clockwise,y 次 horizontal rotate 和z次counterclockwise 之后,原来在n行m列的矩阵的坐标去到哪个位置。
题目意思很容易看懂。易知,对于clockwise,counterclockwise的次数,mod 4 == 0 相当于没有改变!而对于 horizontal rotate,mod 2 == 0 也是没有改变的!
假设问的坐标是(i, j),那么经过一次clockwise的转变,坐标变为(j, n-i+1),交换 n 和 m 值。
经过一次 horizontal rotate,坐标为(i, m-j+1)
经过一次counterclockwise,坐标为(m-j+1, i),交换 n 和 m 值
特别要注意,计算完一个点的位置之后,n和m有可能与原始输入的n和m的值不同,因此需要记录原始输入的 n 和 m !!!因为题目问的是原始输入下的每个坐标点经过x,y,z次转换后的位置,并不是在计算完上一个坐标点后调整过的n 和 m。
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 7 int n, m; 8 int x, y; 9 10 void clockwise(int cnt)11 {12 for (int i = 1; i <= cnt; i++)13 {14 int t = x;15 x = y;16 y = n-t+1;17 swap(n, m);18 }19 }20 21 void hor_rotate()22 {23 y = m - y + 1;24 }25 26 void counterclockwise(int cnt)27 {28 for (int i = 1; i <= cnt; i++)29 {30 int t = x;31 x = m - y + 1;32 y = t;33 swap(n, m);34 }35 }36 37 int main()38 {39 int i, a, b, c, p;40 while (scanf("%d%d%d%d%d%d", &n, &m, &a, &b, &c, &p) != EOF)41 {42 int tn, tm;43 tn = n;44 tm = m;45 for (i = 1; i <= p; i++)46 {47 n = tn;48 m = tm;49 scanf("%d%d", &x, &y);50 if (a % 4)51 clockwise(a%4);52 if (b % 2)53 hor_rotate();54 if (c % 4)55 counterclockwise(c%4);56 printf("%d %d\n", x, y);57 }58 }59 return 0;60 }