题目链接: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 ;
}

NYOJ 298 点的变换的更多相关文章

  1. NYOJ 298 点的变换 矩阵乘法

    http://acm.nyist.net/JudgeOnline/problem.php?pid=298 最好还是自己手推一下矩阵式子..不算太难..但是有一些小知识.... 首先当然是矩阵的细节.. ...

  2. NYOJ 298

    利用矩阵来做变换,参考Max大神的思想的,虽然不是同一道题. ----------- 给定n个点,m个操作,构造O(m+n)的算法输出m个操作后各点的位置.操作有平移.缩放.翻转和旋转    这里的操 ...

  3. NYOJ 298 相变点(矩阵高速功率)

    点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描写叙述 平面上有不超过10000个点.坐标都是已知的.如今可能对全部的点做下面几种操作: 平移一定距离(M),相对X ...

  4. NYOJ 298-点的变换(经典矩阵解决点平移、缩放、翻转和旋转)

    题目地址:NYOJ 298 思路:该题假设用对每一个点模拟的操作.时间复杂度为O(n+m),结果肯定超时.然而利用矩阵乘法能够在O(m)的时间内把全部的操作合并为一个矩阵,然后每一个点与该矩阵相乘能够 ...

  5. NYOJ298 点的变换 【矩阵乘法经典】

    任意门:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298 点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 ...

  6. NYOJ 1007

    在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...

  7. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  8. NYOJ 998

    这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...

  9. Hilbert-Huang Transform(希尔伯特-黄变换)

    在我们正式开始讲解Hilbert-Huang Transform之前,不妨先来了解一下这一伟大算法的两位发明人和这一算法的应用领域 Section I 人物简介 希尔伯特:公认的数学界“无冕之王”,1 ...

随机推荐

  1. js笔记---(运动)通用的move方法,兼容透明度变化

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. Unity-Animator深入系列---测试CrossFade和CrossFadeInFixedTime

    回到 Animator深入系列总目录 CrossFade是按照动画的自身时间进行混合.如果动画10秒,混合持续时间0.2,会在2秒后混合完成 CrossFadeInFixedTime是按照实际时间进行 ...

  3. Android多分辨率适配

    前一阶段开发android项目,由于客户要求进行多分辨率适配,能够支持国内主流的分辨率手机.因此经过了几次开发走了很多弯路,目前刚刚领略了android多分辨率适配的一些方法. 先介绍一下所走的弯路, ...

  4. noip2011提高组day1+day2解题报告

    Day1 T1铺地毯https://www.luogu.org/problem/show?pid=1003 [题目分析] 全部读入以后从最后一个往前找,找到一个矩形的范围覆盖了这个点,那这个矩形就是最 ...

  5. linux 内核手动编译

    手动编译内核 编译时后应安装的支持yum install perlyum install bcyum insatll gcc-c++ .uname -r 先查看内核版本 .yum groupinsta ...

  6. iosanimationWithKeyPath

    animationWithKeyPath的值: transform.scale = 比例轉換     transform.scale.x = 闊的比例轉換     transform.scale.y ...

  7. SqlSever基础 dateadd month 增加五个月

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  8. Web自定义协议,BS端启动CS端,

    实例 1.准备CS项目,windows窗体应用程序,拖进来一个label控件来接受BS的参数,并显示,右击生成,复制该文件的bin目录下的exe,例如放在以下路径,例如C:\\simu\\下, 2.编 ...

  9. iOS深入学习(UITableView:系列1-最基本的东西)

    这是UITableView博客系列的第一篇,使用xib和arc编码,主要讲解一些UITableView使用过程中简单的.但是又容易被忽略的东西,而且我会告诉读者,怎样在使用了之后就再也不会忘记. 操作 ...

  10. Cheatsheet: 2014 03.01 ~ 03.31

    .NET Should I be concerned about PDB files? async and await -Simplified-Internals Web Performance tr ...