题目链接: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. Android之Handler用法总结(1)

    方法一:(java习惯,在android平台开发时这样是不行的,因为它违背了单线程模型) 刚刚开始接触android线程编程的时候,习惯好像java一样,试图用下面的代码解决问题 new Thread ...

  2. c# monitor锁

    当多个线程在并发的时候,难免会碰到相互冲突的事情,比如最经典的ATM机的问题,并发不可怕,可怕的是我们没有能力控制. 线程以我的理解可以分为三种 ① 锁. ② 互斥. ③ 信号. 好,这一篇主要整理“ ...

  3. 一个通用的DAO模型实现增删改查

    首先三个架包: mysql-connector-java-jar commons-dbcp-1.4jar commons-pool-1.5.5jar 导进去: (从上往下一次调用,实现功能) ---- ...

  4. 【linux命令与工具】lsmod命令

    lsmod命令用来显示已被内核加载的模块的状态 描述: lsmod命令可以美观地显示/prco/module中的内容,这些内容是被已被内核加载模块的信息. 使用lsmod之后,系统会显示出目前已经存在 ...

  5. thinkphp 删除多条记录

    删除id为123456的记录 $ids=array(1,2,3,4,5,6);$maps["id"] = array("in",$ids);$this-> ...

  6. CentOS安装zookeeper

    1.zookeeper是个什么玩意? 顾名思义zookeeper就是动物园管理员,他是用来管hadoop(大象).Hive(蜜蜂).pig(小猪)的管理员, Apache Hbase和 Apache  ...

  7. 2016年10月28日 星期五 --出埃及记 Exodus 19:13

    2016年10月28日 星期五 --出埃及记 Exodus 19:13 He shall surely be stoned or shot with arrows; not a hand is to ...

  8. SQL与C#结合完整修改 删除信息

    --SQl中--建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) primary key,- ...

  9. 命令行下运行php的方法和技巧

    linux中直接用"php"命令来执行php文件 一般在linux命令行下运行php文件的代码: XML/HTML代码 linux下执行:#php安装路径 -f php文件路径 例 ...

  10. mvcAPI (入门 3)

    续上 1)无参数Get请求 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...