CF初体验---Round #348,C

来源:互联网 发布:linux启动关闭tomcat 编辑:程序博客网 时间:2024/05/17 09:48

C. Little Artem and Matrix
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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),经过若干种变化,行旋转,列旋转,赋值。得到最终的矩阵。求初始矩阵是多少。
题解:
1.用数组来储存每一组数据(这里使用scanf来输入,特别是数据每种情况下数量不同,需要判断)
2.从最后一组数据开始,暴力向上一步一步模拟(好吧,这里吐槽一下我自己在写代码的时候使用了0行0列,写的时候巨烦。。。。)
3.所谓的旋转,例如行左旋转,就是temp=末尾,然后从倒数第二个开始,前一次向后赋值,最后首个数据=temp,这是最简单的方法
4. 注意输出时:%d后的空格,以及一行输出后的换行。(注意格式)

for(int i=0;i<n;i++){        for(int j=0;j<m;j++){            printf("%d ",matrix[i][j]);        }        cout<<endl;}
#include<iostream>#include<string.h>#include<stdio.h>using namespace std;int main(){int n;int m;int op;cin>>n>>m>>op;int matrix[n][m];memset(matrix,0,sizeof(matrix));int t[10100],r[10100],c[10100],x[10100];for(int i=0;i<op;i++){        scanf("%d",&t[i]);        if(t[i]==1) scanf("%d",&r[i]);        else if(t[i]==2) scanf("%d",&c[i]);        else scanf("%d%d%d",&r[i],&c[i],&x[i]);    }for(int i=op-1;i>=0;i--){    if(t[i]==3)    {    matrix[r[i]-1][c[i]-1]=x[i];    }    else if(t[i]==2) {    int temp;    temp=matrix[n-1][c[i]-1];    for(int j=n-1;j>0;j--){        matrix[j][c[i]-1]=matrix[j-1][c[i]-1];    }    matrix[0][c[i]-1]=temp;    }//changecolumn(matrix,c[i]);    else {    int temp;    temp=matrix[r[i]-1][m-1];    for(int j=m-1;j>0;j--){        matrix[r[i]-1][j]=matrix[r[i]-1][j-1];    }    matrix[r[i]-1][0]=temp;    }//changerow(matrix,r[i]);}for(int i=0;i<n;i++){        for(int j=0;j<m;j++){            printf("%d ",matrix[i][j]);        }        cout<<endl;}}
0 0
原创粉丝点击