【CSP试题回顾】202309-2-坐标变换(其二)

本文介绍了一个CSP编程中的坐标变换算法,通过输入参数执行缩放和平移操作,并使用向量和角度计算新的坐标值。
摘要由CSDN通过智能技术生成

CSP-202309-2-坐标变换(其二)

解题代码

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;

struct MyOpt
{
    double k, theta;
};
int n, m, opt, s, e;
double para, x, y;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    vector<MyOpt>OptList(n + 1, { 1,0 });
    for (size_t i = 1; i <= n; i++)
    {
        cin >> opt >> para;
        if (opt == 1)
        {
            OptList[i].k *= OptList[i - 1].k * para;
            OptList[i].theta = OptList[i - 1].theta;
        }
        else if (opt == 2)
        {
            OptList[i].k = OptList[i - 1].k;
            OptList[i].theta += OptList[i - 1].theta + para;
        }
    }
    for (size_t i = 0; i < m; i++)
    {
        cin >> s >> e >> x >> y;
        x *= (OptList[e].k / OptList[s - 1].k), y *= (OptList[e].k / OptList[s - 1].k);
        double xx = x, yy = y;
        x = xx * cos(OptList[e].theta - OptList[s - 1].theta) - y * sin(OptList[e].theta - OptList[s - 1].theta);
        y = xx * sin(OptList[e].theta - OptList[s - 1].theta) + y * cos(OptList[e].theta - OptList[s - 1].theta);
        cout << fixed << setprecision(3) << x << " " << y << endl;
    }
    return 0;
}

请添加图片描述

根据引用[1]中的描述,本题中操作共有n个,坐标共有m个,且n和m均小于等于100000。因此,暴力算法的时间复杂度为O(n*m),会浪费宝贵的时间。我们需要寻找更高效的算法。 根据题目描述,我们需要进行坐标变换。具体来说,我们需要将一个矩阵中的所有点按照一定规则进行变换。这个规则可以通过观察样例来得到。 假设原始矩阵中的一个点的坐标为(x, y),则变换后的坐标为(x', y'),其中: x' = a1 * x + b1 * y + c1 y' = a2 * x + b2 * y + c2 其中a1, b1, c1, a2, b2, c2是给定的常数。 我们可以将这个变换看作是一个线性变换,即将原始矩阵中的每个点映射到一个新的矩阵中。因此,我们可以使用矩阵乘法来实现这个变换。 具体来说,我们可以将原始矩阵中的每个点表示为一个列向量(x, y, 1),然后将这个列向量与一个3x3的矩阵M相乘,得到一个新的列向量(x', y', w')。其中w'表示一个常数,我们可以将其忽略,因为我们只需要x'和y'。 因此,我们可以将矩阵M表示为: a1 b1 c1 a2 b2 c2 0 1 然后,我们可以将原始矩阵中的每个点表示为一个列向量(x, y, 1),并将这个列向量与矩阵M相乘,得到一个新的列向量(x', y', w')。最后,我们可以将x'和y'作为变换后的坐标。 下面是一个Python实现的例子: ```python import numpy as np # 读入数据 n, m = map(int, input().split()) a = np.zeros((n, 3)) for i in range(n): a[i] = list(map(int, input().split())) # 构造矩阵M a1, b1, c1, a2, b2, c2 = map(int, input().split()) M = np.array([[a1, b1, c1], [a2, b2, c2], [0, 0, 1]]) # 进行坐标变换 b = np.ones((m, 3)) for i in range(m): b[i, :2] = list(map(int, input().split())) c = np.dot(b, M.T) for i in range(m): print(int(c[i, 0]), int(c[i, 1])) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值