题目链接: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. sdp内容解析

    sdp解释 http://datatracker.ietf.org/doc/draft-nandakumar-rtcweb-sdp/?include_text=1

  2. A Round Peg in a Ground Hole(凸包应用POJ 1584)

    A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5684 Accepte ...

  3. Code(组合数学)

    Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8766 Accepted: 4168 Description Tran ...

  4. java单元测试(Junit)

    Eclipse最基本的模块测试 1:首先创建一个java工程,在工程中创建一个被单元测试的Student数据类,如下: package UnitTest; public class Student { ...

  5. linux系统缓存机制

    http://my.oschina.net/lenglingx/blog/425258 1.缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读 ...

  6. shell基础知识

    Shell 学习基础 1.组合命令的符号 管道,将前面一个命令的结果作为后面一个命令的输入 分号,顺序执行用分号分割的命令 重定向,重定向包括三种:输入重定向.输出重定向.错误重定向,以7个不同的符号 ...

  7. 使用js实现移动设备访问跳转到指定目录

    最近最项目的时候总会同时做pc站点跟手机站点,当手机访问的时候默认是看到pc站点的,需要在url上加上/mobile才能正常访问,这段代码是我同事分享给我的,还是蛮实用的. CODE function ...

  8. c++学习之:根据GetLastError()返回值获取错误信息

    VC中GetLastError()获取错误信息的使用在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请用 ...

  9. Linq实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  10. Java-集合框架整理

    一.List 接口集合: 1.优势以及特点:有序,允许重复元素 . 2.实现类: * AarrayList 类:不同步,可变长度数组,倍增率为 1/n ; * LinkedList 类:不同步,链表结 ...