[백준] 17276 배열 돌리기 C++알고리즘/백준2026. 2. 9. 13:35
Table of Contents
https://www.acmicpc.net/problem/17276


해설
주대각 성분은 가운데 열로 이동시키고 가운데 열의 값은 부대각으로 이동, 부대각은 가운데 행으로 이동시키고 가운데 행은 주대각으로 한번씩 이동시키면 되는 문제이다.
각도가 45도의 배수라 했으므로, 회전 횟수를 구하고 45로 나누어서구하고, 회전횟수만큼 45도씩 배열 전체를 회전시키면된다.
각도가 음수인 경우에는 양수로 변환해서 처리해주도록하자.
코드
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <sstream>
#include <set>
#include <unordered_set>
#include <cmath>
using namespace std;
using ll = long long;
#define endl "\n"
const int MAX = 504;
int arr[MAX][MAX];
int ret[MAX][MAX];
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t; cin >> t;
while (t--)
{
int n, deg; cin >> n >> deg;
deg %= 360;
if (deg < 0) deg += 360;
int rotCnt = deg / 45;
// 초기화
for (int i = 0; i < MAX; ++i)
{
for (int j = 0; j < MAX; ++j)
{
arr[i][j] = 0;
ret[i][j] = 0;
}
}
// 입력을 받는다
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cin >> arr[i][j];
while (rotCnt--)
{
// 회전하지 않는 부분에 대해서 ret[i][j]에 값을 복사한다
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
ret[i][j] = arr[i][j];
}
}
// 1. 주대각 -> 가운데 열
for (int i = 1; i <= n; ++i)
{
int a = arr[i][i]; // 주대각 원소
ret[i][(n + 1) / 2] = a;
}
// 2. 가운데 열 -> 부대각 (n, 1), (n - 1, 2) ...
for (int i = 1; i <= n; ++i)
{
int a = arr[i][(n + 1) / 2]; // 가운데 열 원소
ret[i][n - i + 1] = a;
}
// 3. 부대각 -> 중앙행
for (int i = 1; i <= n; ++i)
{
int a = arr[i][n - i + 1]; // 부대각 원소
ret[(n + 1) / 2][n - i + 1] = a;
}
// 4. 중앙행 -> 주대각
for (int i = 1; i <= n; ++i)
{
int a = arr[(n + 1) / 2][i]; // 가운데행 원소
ret[i][i] = a;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
arr[i][j] = ret[i][j];
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
return 0;
}'알고리즘 > 백준' 카테고리의 다른 글
| [백준] 1138 한 줄로 서기 C++ (0) | 2026.02.07 |
|---|---|
| [백준] 20006 랭킹전 대기열 C++ (0) | 2026.02.06 |
| [백준] 1913 달팽이 C++ (0) | 2026.02.05 |
| [백준] 20546 🐜 기적의 매매법 🐜 C++ (0) | 2026.02.04 |
| [백준] 풍선 터뜨리기 C++ (0) | 2026.02.02 |
@CGNY :: 김놀자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!