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"); } }
新聞熱點
疑難解答