题目链接:298 点的变换
这题放在矩阵快速幂里,我一开始想不透它是怎么和矩阵搭上边的,然后写了个暴力的果然超时,上网看了题解后,发现竟然能够构造一些精巧的矩阵来处理,不得不说实在太强大了! http://blog.csdn.net/lyhvoyage/article/details/39755595
然后我的代码是:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
using namespace std;
#define For(i,s,t) for(int i=s; i<=t; ++i)
const double pi= acos(-1.0); struct matrix{
int r,c;
double a[][];
matrix(): r(),c(){
memset(a,,sizeof(a));
a[][]= 1.0;
}
matrix(char ch){
new (this)matrix();
if(ch=='x' || ch=='X'){
a[][]= 1.0;
a[][]= -1.0;
}
else if(ch=='y' || ch== 'Y'){
a[][]= 1.0;
a[][]= -1.0;
}
else if(ch=='E'){
a[][]= a[][]= 1.0;
}
}
matrix(double p, char ch){
new (this)matrix();
if(ch=='s' || ch== 'S'){
a[][]= a[][]= p;
}
else if(ch=='r' || ch=='R'){
a[][]= a[][]= cos(p);
a[][]= sin(p);
a[][]= -sin(p);
}
}
matrix(double dx, double dy, char ch){
if(ch=='p'|| ch=='P'){
new (this)matrix();
r= ;
a[][]= dx;
a[][]= dy;
a[][]= 1.0;
}
else if(ch=='M' || ch=='m'){
new (this)matrix();
a[][]= a[][]= 1.0;
a[][]= dx;
a[][]= dy;
}
}
matrix operator *(const matrix &m2) const {
matrix mul;
mul.r= r;
mul.c= m2.c;
mul.a[][]= 0.0;
For(i,,r) For(j,,m2.c) For(k,,c)
mul.a[i][j]+= a[i][k]*m2.a[k][j];
return mul;
}
} point[]; int main()
{
int n,m;
scanf("%d%d",&n,&m);
double x,y;
for(int i=; i<=n; ++i){
scanf("%lf %lf",&x,&y);
point[i]= matrix(x,y,'p');
}
matrix ans('E');
while(m--){
char ch;
cin>>ch;
if(ch =='X') ans= ans*matrix('X');
else if(ch=='Y') ans= ans*matrix('Y');
else if(ch=='M'){
double dx,dy;
scanf("%lf %lf",&dx,&dy);
ans= ans*matrix(dx,dy,'M');
}
else if(ch=='S'){
double p;
scanf("%lf",&p);
ans= ans*matrix(p,'S');
}
else if(ch=='R'){
double rad;
scanf("%lf",&rad);
rad= rad/*pi;
ans= ans*matrix(rad,'R');
}
}
for(int i=; i<=n; ++i){
point[i]= point[i]*ans;
printf("%.1f %.1f\n",point[i].a[][],point[i].a[][]);
}
return ;
}
还是第一次写了超过100行的代码,调试了好久了,唉~~
然后,稍微作了下更改,还有另一个版本的:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
#define For(i,s,t) for(int i=s; i<=t; ++i)
const double pi= acos(-1.0); struct matrix{
int r,c;
double a[][];
void init(int r=, int c=){
this->r = r;
this->c = c;
memset(a,,sizeof(a));
a[][]= 1.0;
}
matrix() { init(); }
matrix(char ch){
init();
if(ch=='x' || ch=='X'){
a[][]= 1.0;
a[][]= -1.0;
}
else if(ch=='y' || ch== 'Y'){
a[][]= 1.0;
a[][]= -1.0;
}
else if(ch=='E'){
a[][]= a[][]= 1.0;
}
}
matrix(double p, char ch){
init();
if(ch=='s' || ch== 'S'){
a[][]= a[][]= p;
}
else if(ch=='r' || ch=='R'){
a[][]= a[][]= cos(p);
a[][]= sin(p);
a[][]= -sin(p);
}
}
matrix(double dx, double dy, char ch){
if(ch=='p'|| ch=='P'){
init(,);
a[][]= dx;
a[][]= dy;
a[][]= 1.0;
}
else if(ch=='M' || ch=='m'){
init();
a[][]= a[][]= 1.0;
a[][]= dx;
a[][]= dy;
}
}
matrix operator *(const matrix &m2) const {
matrix mul;
mul.init(r,m2.c);
mul.a[][]= 0.0;
For(i,,r) For(j,,m2.c) For(k,,c)
mul.a[i][j]+= a[i][k]*m2.a[k][j];
return mul;
}
} point[]; int main()
{
int n,m;
scanf("%d%d",&n,&m);
double x,y;
for(int i=; i<=n; ++i){
scanf("%lf %lf",&x,&y);
point[i]= matrix(x,y,'p');
}
matrix ans('E');
while(m--){
getchar();
char ch= getchar();
if(ch =='X') ans= ans*matrix('X');
else if(ch=='Y') ans= ans*matrix('Y');
else if(ch=='M'){
double dx,dy;
scanf("%lf %lf",&dx,&dy);
ans= ans*matrix(dx,dy,'M');
}
else if(ch=='S'){
double p;
scanf("%lf",&p);
ans= ans*matrix(p,'S');
}
else if(ch=='R'){
double rad;
scanf("%lf",&rad);
rad= rad/*pi;
ans= ans*matrix(rad,'R');
}
}
for(int i=; i<=n; ++i){
point[i]= point[i]*ans;
printf("%.1f %.1f\n",point[i].a[][],point[i].a[][]);
}
return ;
}