天天看点

BUPT OJ|304. 旋转图像-计算机一2014

题目来源 北邮OJ

BUPT OJ|304. 旋转图像-计算机一2014

每次循环顺时针旋转矩阵90°

#include<iostream>
#include<cstring>
using namespace std;
/*Input:
2
2 3
111
000
90
3 3
111
101
111
180 
------------270°测试-----------
1
2 3
111
000
270
*/ 
char a[100][100];
char b[100][100];
int N,M;
void rotate(int rad){
	int temp,r,i,j;
	for(r=0;r<rad/90;r++){
		for(i=0;i<N;i++){
			for(j=0;j<M;j++){
				b[j][N-i-1]=a[i][j];
			}
		}
		temp=N,N=M,M=temp;
		for(i=0;i<N;i++){
			for(j=0;j<M;j++){
				a[i][j]=b[i][j];
			}
		}
	} 
	for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			cout<<a[i][j];
		}
		cout<<endl;
	}
}
int main()
{
	int T,rad;
	cin>>T;

	while(T--){
		cin>>N>>M;
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				cin>>a[i][j];
			}
		}
		cin>>rad;
		rotate(rad); 
	}
}
           

继续阅读