NYOJ 298 点的变换
题目链接: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 点的变换的更多相关文章
- NYOJ 298 点的变换 矩阵乘法
http://acm.nyist.net/JudgeOnline/problem.php?pid=298 最好还是自己手推一下矩阵式子..不算太难..但是有一些小知识.... 首先当然是矩阵的细节.. ...
- NYOJ 298
利用矩阵来做变换,参考Max大神的思想的,虽然不是同一道题. ----------- 给定n个点,m个操作,构造O(m+n)的算法输出m个操作后各点的位置.操作有平移.缩放.翻转和旋转 这里的操 ...
- NYOJ 298 相变点(矩阵高速功率)
点的变换 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描写叙述 平面上有不超过10000个点.坐标都是已知的.如今可能对全部的点做下面几种操作: 平移一定距离(M),相对X ...
- NYOJ 298-点的变换(经典矩阵解决点平移、缩放、翻转和旋转)
题目地址:NYOJ 298 思路:该题假设用对每一个点模拟的操作.时间复杂度为O(n+m),结果肯定超时.然而利用矩阵乘法能够在O(m)的时间内把全部的操作合并为一个矩阵,然后每一个点与该矩阵相乘能够 ...
- NYOJ298 点的变换 【矩阵乘法经典】
任意门:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298 点的变换 时间限制:2000 ms | 内存限制:65535 KB 难度:5 ...
- NYOJ 1007
在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- NYOJ 998
这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...
- Hilbert-Huang Transform(希尔伯特-黄变换)
在我们正式开始讲解Hilbert-Huang Transform之前,不妨先来了解一下这一伟大算法的两位发明人和这一算法的应用领域 Section I 人物简介 希尔伯特:公认的数学界“无冕之王”,1 ...
随机推荐
- 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...
- Java学习之路(七)
1:什么是异常? 中断了正常指令流的事件. 异常是一个对象 ,在出现异常时,虚拟机会生成一个异常对象 生成对象的类是由 JDK 提供的
- dubbo模块组织方式
dubbo源码版本:2.5.4 阿里通过maven将dubbo的36个模块组织成了一个项目,各个模块结构如下: -------------------------------------------- ...
- redis pool
Redis Pool--Java 配置文件 #redis conf ADDR=127.0.0.1 PORT= AUTH= #session timeout TIMEOUT= MAX_ACTIVE= M ...
- __NSCFConstantString
-[__NSCFConstantString size]: unrecognized selector sent to instance 0x53ea70 该错误是在我将NSString类型的参数赋值 ...
- R语言画图基础参数设置
Graphical Parameters You can customize many features of your graphs (fonts, colors, axes, titles) th ...
- 【转】The decoupling capacitor…is it really necessary?
Before working as an applications engineer, I worked as an IC test development engineer here at TI. ...
- Beaglebone Black–智能家居控制系统 LAS - 用 UART 连接 ESP8266 (ESP-01 版)
这是一块便宜 (¥12.5)的 WiFi 模块,3.3V ,芯片是乐鑫科技(Espressif)出品.它本身是很多玩法,比如这个 NodeMCU (淘宝有套件焊接好一整套的带 USB 接口的,搜 es ...
- [数据结构与算法]栈Stack的多种实现
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Codeforces Round #260 (Div. 2)AB
http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...