生活随笔
收集整理的這篇文章主要介紹了
镜像 模拟
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題 E: 鏡像
時間限制: 1 Sec 內存限制: 128 MB
題目描述
群星羅列者 beny 在通過冒險模式中的克隆大師澤里克的鏡像關卡時,遇到了難題:
他現在面前陳列著許多的星星,這些星星排列成一個正方形矩陣,并從左到右從上到下依次編號為 1~n*n。
偉大而又陰陽怪氣的克隆大師澤里克向 beny 提出了一次操作要求:
將某一個子正方形里的星星旋轉 90 度。
澤里克要你排列出操作后的星星矩陣并輸出。
輸入
第一行輸入兩個正整數 n, Q,星星矩陣的邊長。
接下來 Q 行,每行三個數 x, y, z,表示一組操作:
將左上角為(x,y),邊長為 z 的子矩陣順時針旋轉 90 度。
保證所有操作合法。
輸出
輸出所有操作完后n行n列的矩陣。
樣例輸入 Copy
4 2
1 1 4
3 3 2
樣例輸出 Copy
13 9 5 1
14 10 6 2
15 11 8 7
16 12 4 3
提示
對于前20%的數據n<=20
對于前60%的數據Q=1
對于100%的數據n<=100,Q<=200
其中有30%的數據保證z=2
按照題意模擬就好了,數據比較小,可以寫一個reverse函數,對于每一層都執行函數即可,因為每一次要翻轉的可以分為 z / 2 層。
再就是翻轉的時候注意只需要枚舉 l - 1 個即可,因為每一行或一列的最后一位都是下一列或一行的第一位。
注意一下細節吧,debug了半天發現賦值賦錯了。。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
using namespace std
;typedef long long LL
;
typedef pair
<int,int> PII
;const int N
=110,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;int n
,q
;
int mp
[N
][N
];
int tem
[N
];void print()
{for(int i
=1;i
<=n
;i
++){for(int j
=1;j
<=n
;j
++)printf("%d ",mp
[i
][j
]);puts("");}
}void rev(int x
,int y
,int l
)
{int cnt
=0;for(int i
=y
;i
<y
+l
-1;i
++)tem
[++cnt
]=mp
[x
][i
];for(int i
=1;i
<=l
-1;i
++){swap(mp
[x
+i
-1][y
+l
-1],tem
[i
]);swap(mp
[x
+l
-1][y
+l
-i
],tem
[i
]);swap(mp
[x
+l
-i
][y
],tem
[i
]);swap(mp
[x
][y
+i
-1],tem
[i
]);}
}int main()
{
scanf("%d%d",&n
,&q
);int p
=1;for(int i
=1;i
<=n
;i
++)for(int j
=1;j
<=n
;j
++)mp
[i
][j
]=p
++;while(q
--){int x
,y
,z
;scanf("%d%d%d",&x
,&y
,&z
);for(int i
=0;i
<z
/2;i
++)rev(x
+i
,y
+i
,z
-i
*2);}print();return 0;
}
總結
以上是生活随笔為你收集整理的镜像 模拟的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。