Codeforces Round #348 DIV2 C. Little Artem and Matrix(将矩阵的第x行向左边旋转一位,将矩阵的第y列向上边旋转一位,现在的第x,y位置为z)

C. Little Artem and Matrix
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.

That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to mfrom left to right.

Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.

Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.

Input

The first line of the input contains three integers nm and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively.

Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers rici and xi (1 ≤ ri ≤ n1 ≤ ci ≤ m - 109 ≤ xi ≤ 109) are given.

Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.

Output

Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.

If there are multiple valid solutions, output any of them.

Examples
input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
output
8 2 
1 8 
input
3 3 2
1 2
3 2 2 5
output
0 0 0 
0 0 5 
0 0 0 


题意:给你一个矩阵,这个矩阵有三个操作
1.将矩阵的第x行向左边旋转一位
2.将矩阵的第y列向上边旋转一位
3.现在的第x,y位置是z
求最初的矩阵


思路:
倒着做,如果是操作3直接赋值,如果是操作1,使现在的a[i][j]=a[i][j-1],
如果是操作2,使现在的a[i][j]=a[i-1][j]

#include<bits/stdc++.h>
using namespace std;
const int maxn=10010;
int a[110][110];
struct query{
    int op,x,y,num;
}Q[maxn];

int main(){
    int n,m,q,x,y,num,op;
    scanf("%d%d%d",&n,&m,&q);
    int cnt=0;
    for(int i=1;i<=q;i++){
        scanf("%d",&op);
        if(op==1||op==2){
            scanf("%d",&x);
            Q[cnt].op=op,Q[cnt].x=x;
            cnt++;
        }
        else{
            scanf("%d%d%d",&x,&y,&num);
             Q[cnt].op=op,Q[cnt].x=x,Q[cnt].y=y,Q[cnt].num=num;
             cnt++;
        }
    }
    for(int i=cnt-1;i>=0;i--){
        if(Q[i].op==3)
            a[Q[i].x][Q[i].y]=Q[i].num;
        else if(Q[i].op==1){
            num=a[Q[i].x][m];
            for(int j=m;j>=2;j--)
                a[Q[i].x][j]=a[Q[i].x][j-1];
            a[Q[i].x][1]=num;
        }
        else{
            num=a[n][Q[i].x];
            for(int j=n;j>=2;j--)
                a[j][Q[i].x]=a[j-1][Q[i].x];
            a[1][Q[i].x]=num;
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d",a[i][1]);
        for(int j=2;j<=m;j++)
            printf(" %d",a[i][j]);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值