文章

34

粉丝

0

获赞

6

访问

999

头像
旋转矩阵 题解:
P1221 同济大学机试题
发布于2025年8月4日 20:11
阅读数 44

矩阵旋转题目,把握好顺时针旋转90度的逻辑、和纵向对折交换即可

tip:建议采用vector容器,便于数组处理

#include<bits/stdc++.h>
using namespace std;

// 矩阵顺时针旋转90度
vector<vector<int>> rotate90(const vector<vector<int>> &a){
	int n = a.size();
	int m = a[0].size();
	vector<vector<int>> res(m, vector<int>(n));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) res[j][n - 1 - i] = a[i][j];
	
	return res;
}

// 矩阵沿纵向对称轴翻折
vector<vector<int>> fold(const vector<vector<int>> &a){
	int n = a.size();
	int m = a[0].size();
	vector<vector<int>> res(n, vector<int>(m));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) res[i][m - 1 - j] = a[i][j];
	
	return res;
}

// 矩阵逆时针旋转90度,即顺时针旋转270度
vector<vector<int>> inv_rotate90(const vector<vector<int>> &a){
	return rotate90(rotate90(rotate90(a)));
}

int main(){
	
	int n, m, k;
	
	while(cin >> n >&g...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发