CSP 202212-3 JPEG 解码

写的好麻烦,还需要优化,啥时候才能优雅地写出t3啊....

#include<bits/stdc++.h>
using namespace std;
const double PI = 3.1415926;
vector<vector<int>> Q(8,vector<int>(8)),M(8,vector<int>(8));
vector<vector<int>> ldir= {{0,1},{1,-1},{1,0},{-1,1}};
vector<vector<int>> rdir = {{0,1},{-1,1},{1,0},{1,-1}};
void readQ() {
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 8; j++) {
            cin >> Q[i][j];
        }
    }
}
void Incre (vector<vector<int>>& dir,int num,int& x, int& y,int& idx) {
    M[x][y] = num;
    if(idx == 1 || idx == 3) {
        int xx = x + dir[idx][0];
        int yy = y + dir[idx][1];
        if (xx < 0 || xx > 7 || yy < 0 || yy > 7) { idx = ((idx + 1) % 4); }
        else {
            x = x + dir[idx][0];
            y = y + dir[idx][1];
            return;
        }

    }
    x = x + dir[idx][0];
    y = y + dir[idx][1];
    idx = ((idx + 1) % 4);
};
void readM(int n) {
    int x = 0, y = 0;
    int idx = 0;
    for(int i = 1; i <= n; i++) {
        int num; cin >> num;
        if(i == 36) {
            idx = 1;
            M[7][0] = num;
            x = 7, y = 1;
        } else if(i > 36) {
            Incre(rdir,num,x,y,idx);
        } else {
            Incre(ldir,num,x,y,idx);
        }
    }
}
void stepOne() {
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 8; j++) {
            M[i][j] *= Q[i][j];
        }
    }
}
vector<vector<double>> stepTwo() {
    vector<vector<double>> ret(8,vector<double>(8));
    auto calc = [&](int i, int j) {
        double ret = 0;
        for(int u = 0; u < 8; u++) {
            for(int v = 0; v < 8; v++) {
                double au = u == 0 ? sqrt(0.5) : 1;
                double av = v == 0 ? sqrt(0.5) : 1;
                ret = ret + M[u][v] * au * av * cos(PI / 8 * (i + 0.5) * u) * cos(PI / 8 * (j + 0.5) * v);
            }
        }
        return ret / 4;
    };
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 8; j++) {
            ret[i][j] = calc(i,j);
        }
    }
    return ret;
}
vector<vector<int>> stepThree(vector<vector<double>>& Mq){
    vector<vector<int>> ret(8,vector<int>(8));
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 8; j++) {
            int ins =  ((Mq[i][j] + 128) + 0.5);
            if(ins > 255) ins = 255;
            if(ins < 0) ins = 0;
            ret[i][j] = ins;
        }
    }
    return ret;
}
template<typename T>
void print(vector<vector<T>>& mar) {
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 8; j++) {
            cout << mar[i][j] << ' ';
        }
        cout << '\n';
    }
}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    readQ();
    int n,T;
    cin >> n;
    cin >> T;
    readM(n);
    if(T == 0) {
        print(M);
    } else if(T == 1) {
        stepOne();
        print(M);
    }  else {
        stepOne();
        auto tmp = stepTwo();
        auto ans = stepThree(tmp);
        print(ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值