Codeforces 400 C. Inna and Huge Candy Matrix【 Codeforces Round #234 (Div. 2)】

简介:
C. Inna and Huge Candy Matrix
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).

The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like.

Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!

Input

The first line of the input contains fix integers nmxyzp (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105).

Each of the following p lines contains two integers xkyk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.

Output

For each of the p candies, print on a single line its space-separated new coordinates.

Sample test(s)
input
3 3 3 1 1 9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
output
1 3
1 2
1 1
2 3
2 2
2 1
3 3
3 2
3 1
Note

Just for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:

QWER      REWQ 
ASDF  ->  FDSA
ZXCV      VCXZ

题目大意:

给你一个 n 行 m 列的矩阵,然后对这个矩阵顺时针旋转 x 次,水平旋转 y 次,逆时针旋转 z 次。然后有
p 个坐标,让你求这 p 个坐标旋转完之后的坐标。。。

解题思路:

找规律,
当为顺时针旋转的时候
旋转次数        原坐标         旋转之后的坐标
1                   (x, y)            (y,n+1-x)
2                   (x, y)            (n+1-x, m+1-y)
3                   (x, y)            (m+1-y, x)

当为水平旋转的时候
旋转次数        原坐标         旋转之后的坐标
1                   (x, y)            (x,m+1-y)


当为顺时针旋转的时候
旋转次数        原坐标         旋转之后的坐标
1                   (x, y)            (m+1-y, x)
2                   (x, y)            (n+1-x, m+1-y)
3                   (x, y)              (y,n+1-x)

然后注意一下细节,就是没转完一次 m 和 n 要交换【当顺时针和逆时针为奇数的时候】
还有就是不要顺时针和逆时针抵消,因为有水平旋转。。。
上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5+5;
const int mod = 1000000007;
const double eps = 1e0-7;
int xx[maxn], yy[maxn], tmp[maxn];
int main()
{
    int n, m, x, y, z, p;
    while(~scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&z,&p))
    {
        x %= 4, y%=2, z%=4;
        ///int ans = x - z;
        int mm = m, nn = n;
        for(int i=0; i<p; i++)
            scanf("%d%d",&xx[i],&yy[i]);
        for(int i=0; i<p; i++)
        {
            m = mm,  n = nn;
            if(x == 1)
            {
                tmp[i] = xx[i];
                xx[i] = yy[i];
                yy[i] = n+1-tmp[i];
                swap(m, n);
            }
            else if(x == 2)
            {
                xx[i] = n+1-xx[i];
                yy[i] = m+1-yy[i];
            }
            else if(x == 3)
            {
                tmp[i] = xx[i];
                xx[i] = m+1-yy[i];
                yy[i] = tmp[i];
                swap(m, n);
            }
            if(y == 1)
                yy[i] = m+1-yy[i];
            if(z == 1)
            {
                tmp[i] = xx[i];
                xx[i] = m+1-yy[i];
                yy[i] = tmp[i];
                swap(m, n);
            }
            else if(z == 2)
            {
                xx[i] = n+1-xx[i];
                yy[i] = m+1-yy[i];
            }
            else if(z == 3)
            {
                tmp[i] = xx[i];
                xx[i] = yy[i];
                yy[i] = n+1-tmp[i];
                swap(m, n);
            }
        }
        for(int i=0; i<p; i++)
            cout<<xx[i]<<" "<<yy[i]<<endl;
    }
    return 0;
}




目录
相关文章
Codeforces Round #192 (Div. 2) (330B) B.Road Construction
要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目。 要求两个城市之间不超过两条道路,那么所有的城市应该是连在一个点上的,至于这个点就很好找了,只要找到一个没有和其他点有道路限制的即可。
46 0
【CodeForces】Codeforces Round 857 (Div. 2) B
【CodeForces】Codeforces Round 857 (Div. 2) B
136 0
|
机器学习/深度学习 Java
codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题
刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。
121 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output ...
1250 0