NYOJ 298-點的變換(經典矩陣解決點平移、縮放、翻轉和旋轉),nyoj298-

來源:互聯網
上載者:User

NYOJ 298-點的變換(經典矩陣解決點平移、縮放、翻轉和旋轉),nyoj298-

題目地址:NYOJ 298

思路:該題如果用對每個點類比的操作,時間複雜度為O(n+m),結果肯定逾時。然而利用矩陣乘法可以在O(m)的時間內把所有的操作合并為一個矩陣,然後每個點與該矩陣相乘可以得出最終的位置。

PS:十個利用矩陣乘法解決的經典題目 超級詳細。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=1e5+10;struct node {    double mp[5][5];} q[maxn],res,init,ans;struct node Mul(struct node x,struct node y) {    struct node tmp;    int i,j,k;    for(i=0; i<3; i++) {        for(j=0; j<3; j++) {            tmp.mp[i][j]=0;            for(k=0; k<3; k++)                tmp.mp[i][j]=tmp.mp[i][j]+x.mp[i][k]*y.mp[k][j];        }    }    return tmp;}int main() {    int n,m,i,j;    double a,b,bs,jd,hd;    while(~scanf("%d %d",&n,&m)) {        for(i=0; i<n; i++) {            scanf("%lf %lf",&q[i].mp[0][0],&q[i].mp[1][0]);            q[i].mp[2][0]=1;        }        for(i=0; i<3; i++) {            ans.mp[i][i]=1;        }        char str[10];        while(m--) {            memset(str,0,sizeof(str));            memset(res.mp,0,sizeof(res.mp));            scanf("%s",str);            for(i=0; i<3; i++)                res.mp[i][i]=1;            if(str[0]=='M') {                scanf("%lf %lf",&a,&b);                res.mp[0][2]=a;                res.mp[1][2]=b;            } else if(str[0]=='X') {                res.mp[1][1]=-1;            } else if(str[0]=='Y') {                res.mp[0][0]=-1;            } else if(str[0]=='S') {                scanf("%lf",&bs);                res.mp[0][0]=bs;                res.mp[1][1]=bs;            } else if(str[0]=='R') {                scanf("%lf",&jd);                hd=jd/180*pi;                res.mp[0][0]=cos(hd);                res.mp[0][1]=-sin(hd);                res.mp[1][0]=sin(hd);                res.mp[1][1]=cos(hd);            }            ans=Mul(res,ans);        }        for(i=0; i<n; i++) {            init=Mul(ans,q[i]);            printf("%.1lf %.1lf\n",init.mp[0][0],init.mp[1][0]);        }    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.