codeforces C. Inna and Huge Candy Matrix(模拟)

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.

Alt

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 n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105).

Each of the following p lines contains two integers xk, yk (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.

Examples

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 次顺时针旋转90°,y 次左右翻转,z 次逆时针旋转90°;然后按输入顺序作为先后标号,对变换后的糖果位置进行输出。
对x, y, z 进行取模(因为有周期规律),只需要把三种变换都写出来,按照需要操作的次数直接进行需要次数的变换就行了。撑死也就7次变换,不会超时的。
记得这是个矩阵,旋转之后要调换行列数值。

代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
#define MSC(X) memset(X,-1,sizeof(X))
typedef long long LL;
using namespace std;
const int maxp=1e5+5;
struct candy{
    int r,c;
}a[maxp];
int n,m,x,y,z,p;
int main(){
    scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&z,&p);
    x%=4;y%=2;z%=4;
    for(int i=1;i<=p;i++){
        scanf("%d%d",&a[i].r,&a[i].c);
    }
    for(int i=0;i<x;i++){
        for(int j=1;j<=p;j++){
            int t=a[j].c;
            a[j].c=n-a[j].r+1;
            a[j].r=t;
        }
        int t=m;
        m=n;
        n=t;
    }
    if(y!=0) for(int i=1;i<=p;i++)
                a[i].c=m-a[i].c+1;
    for(int i=0;i<z;i++){
        for(int j=1;j<=p;j++){
            int t=a[j].r;
            a[j].r=m-a[j].c+1;
            a[j].c=t;
        }
        int t=m;
        m=n;
        n=t;
    }
    for(int i=1;i<=p;i++){
        printf("%d %d\n",a[i].r,a[i].c);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值