1. 程式人生 > >NYOJ298 點的變換 【矩陣乘法經典】

NYOJ298 點的變換 【矩陣乘法經典】

任意門:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298

點的變換

時間限制: 2000 ms  |  記憶體限制:65535 KB 難度: 5  
描述

平面上有不超過10000個點,座標都是已知的,現在可能對所有的點做以下幾種操作:

平移一定距離(M),相對X軸上下翻轉(X),相對Y軸左右翻轉(Y),座標縮小或放大一定的倍數(S),所有點對座標原點逆時針旋轉一定角度(R)。    

操作的次數不超過1000000次,求最終所有點的座標。

 

提示:如果程式中用到PI的值,可以用acos(-1.0)獲得。

 
輸入
只有一組測試資料
測試資料的第一行是兩個整數N,M,分別表示點的個數與操作的個數(N<=10000,M<=1000000)
隨後的一行有N對數對,每個數對的第一個數表示一個點的x座標,第二個數表示y座標,這些點初始座標大小絕對值不超過100。
隨後的M行,每行代表一種操作,行首是一個字元:
首字元如果是M,則表示平移操作,該行後面將跟兩個數x,y,表示把所有點按向量(x,y)平移;
首字元如果是X,則表示把所有點相對於X軸進行上下翻轉;
首字元如果是Y,則表示把所有點相對於Y軸進行左右翻轉;
首字元如果是S,則隨後將跟一個數P,表示座標放大P倍;
首字元如果是R,則隨後將跟一個數A,表示所有點相對座標原點逆時針旋轉一定的角度A(單位是度)
輸出
每行輸出兩個數,表示一個點的座標(對結果四捨五入到小數點後1位,輸出一位小數位)
點的輸出順序應與輸入順序保持一致
樣例輸入
2 5
1.0 2.0 2.0 3.0
X
Y
M 2.0 3.0
S 2.0
R 180
樣例輸出
-2.0 -2.0
0.0 0.0

 

題意概括:如題幹

解題思路:

點的變換即向量的變換,即轉換為矩陣運算,因為就是做初等變換嘛。

 

 

AC code:

  1 //題目連結 http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298
2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstring> 6 #include <cmath> 7 #define LL long long 8 using namespace std; 9 const int MAXN = 5; 10 const int MAXS = 1e4+2; 11 const double pi = acos(-1.0); 12 int N, M; 13 14 struct mat 15 { 16 double m[MAXN][MAXN]; 17 }base; 18 mat pp[MAXS]; 19 20 mat muti(mat a, mat b) 21 { 22 mat res; 23 memset(res.m, 0, sizeof(res.m)); 24 for(int i = 1; i <= 3; i++){ 25 for(int j = 1; j <= 3; j++) 26 if(a.m[i][j]){ 27 for(int k = 1; k <= 3; k++) 28 res.m[i][k] = (res.m[i][k] + a.m[i][j] * b.m[j][k]); 29 } 30 } 31 return res; 32 } 33 34 mat add(mat a, mat b) 35 { 36 mat res; 37 memset(res.m, 0, sizeof(res.m)); 38 for(int i = 1; i <= 3; i++){ 39 for(int j = 1; j <= 3; j++){ 40 res.m[i][j] = (a.m[i][j] + b.m[i][j]); 41 } 42 } 43 return res; 44 } 45 46 //mat qpow(mat a, int n) 47 //{ 48 // mat res; 49 // memset(res.m, 0, sizeof(res.m)); 50 // for(int i = 1; i <= 3; i++) res.m[i][i] = 1; 51 // 52 // while(n){ 53 // if(n&1) res = muti(res, a); 54 // n>>=1; 55 // a = myti(a, a); 56 // } 57 // return res; 58 //} 59 60 int main() 61 { 62 char com; 63 mat tmp; 64 double a, b; 65 scanf("%d %d", &N, &M); 66 for(int i = 1; i <= N; i++){ 67 scanf("%lf %lf", &a, &b); 68 pp[i].m[1][1] = a; 69 pp[i].m[2][1] = b; 70 pp[i].m[3][1] = 1; 71 } 72 memset(tmp.m, 0, sizeof(tmp.m)); 73 for(int i = 1; i <= 3; i++) tmp.m[i][i] = 1; 74 while(M--){ 75 memset(base.m, 0, sizeof(base.m)); 76 for(int i = 1; i <= 3; i++) base.m[i][i] = 1; 77 getchar(); 78 scanf("%c", &com); 79 if(com == 'M'){ 80 scanf("%lf %lf", &a, &b); 81 base.m[1][3] = a; 82 base.m[2][3] = b; 83 } 84 else if(com == 'X'){ 85 base.m[2][2] = -1; 86 } 87 else if(com == 'Y'){ 88 base.m[1][1] = -1; 89 } 90 else if(com == 'S'){ 91 scanf("%lf", &a); 92 base.m[1][1] = a; 93 base.m[2][2] = a; 94 } 95 else if(com == 'R'){ 96 scanf("%lf", &a); 97 a = a/180*pi; 98 base.m[1][1] = cos(a); 99 base.m[1][2] = -sin(a); 100 base.m[2][1] = sin(a); 101 base.m[2][2] = cos(a); 102 } 103 tmp = muti(base, tmp); 104 } 105 mat ans; 106 for(int i = 1; i <= N; i++){ 107 // pp[i] = muti(tmp, pp[i]); 108 // printf("%.1f %.1f\n", pp[i].m[1][1], pp[i].m[2][1]); 109 ans = muti(tmp, pp[i]); 110 printf("%.1f %.1f\n", ans.m[1][1], ans.m[2][1]); 111 } 112 return 0; 113 }
View Code