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 from1 to n from top to bottom, while columns are numbered with integers from1 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 ofq 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.
InputThe 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 integerti (1 ≤ ti ≤ 3) that defines the type of the Operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) orci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integersri,ci andxi (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 rowri, that is it will shift cyclically. Operation of the second type (ti = 2) means that columnci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the rowri and columnci stores valuexi.
OutputPRint the description of any valid initial matrix as n lines containingm integers each. All output integers should not exceed109 by their absolute value.
If there are multiple valid solutions, output any of them.
ExamplesInput2 2 62 12 23 1 1 13 2 2 23 1 2 83 2 1 8Output8 2 1 8 Input3 3 21 23 2 2 5Output0 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"); } }
新闻热点
疑难解答