Codeforces 669C Little Artem and Matrix【思维+暴力】

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 m from 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 n, m 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 ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ 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 

题目大意:

我们需要找到一个合法的原式矩阵,其规格为N*M的大小。

我们已知对于原矩阵的Q次操作的信息:

1 x表示对于第x行来说,我们将第一个元素挪到最后边,然后依次将第二个元素放到第一的位子上去,第三个元素放到第二的位子上去..................

2 x表示对于第x列来说,我们将第一个元素挪到最后边,然后依次将第二个元素放到第一的位子上去,第三个元素放到第二的位子上去..................

3 x y z 表示此时位子(x,y)处的值是z.


思路:


不难的一道题,暴力处理操作的时间复杂度对于操作1来说是O(m),对于操作2来说是O(n),操作3来说是O(1);

对于操作3来讲,就是告诉我们此时矩阵元素的信息的操作。那么我们通过逆序进行所有操作,就能够得到原来矩阵的样子。

那么我们再观察到N.M的大小:【1,100】;那么我们暴力处理即可,O(100q)是肯定不会超时的。

那么对于操作1和操作2来讲,我们应该依次将第1个元素放到第2的位子上去,第2个元素放到第3的位子上去,最后将最后一个元素放到第1的位子上去即可。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
    int op,x,y,z;
}a[1000060];
int ans[105][105];
int main()
{
    int n,m,q;
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        memset(ans,0,sizeof(ans));
        for(int i=0;i<q;i++)
        {
            scanf("%d",&a[i].op);
            if(a[i].op==1||a[i].op==2)
            {
                scanf("%d",&a[i].x);
                a[i].x--;
            }
            else scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z),a[i].x--,a[i].y--;
        }
        for(int z=q-1;z>=0;z--)
        {
            if(a[z].op==1)
            {
                int tmp=ans[a[z].x][m-1];
                for(int i=m-1;i>=1;i--)
                {
                    ans[a[z].x][i]=ans[a[z].x][i-1];
                }
                ans[a[z].x][0]=tmp;
            }
            if(a[z].op==2)
            {
                int tmp=ans[n-1][a[z].x];
                for(int i=n-1;i>=1;i--)
                {
                    ans[i][a[z].x]=ans[i-1][a[z].x];
                }
                ans[0][a[z].x]=tmp;
            }
            if(a[z].op==3)
            {
                ans[a[z].x][a[z].y]=a[z].z;
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                printf("%d ",ans[i][j]);
            }
            printf("\n");
        }
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值