백준 16927번 배열 돌리기 2 문제풀이(C++)

YooHeeJoon·2023년 1월 24일
0

백준 문제풀이

목록 보기
46/56

백준 16927번 배열 돌리기 2

아이디어

배열 회전 시킨다 ==> 배열 두개 만들어서 a[0][0] = b[0][1] 이런 느낌으로 담자!

테두리의 인덱스 변화는 직관적으로 구현해보자

if (y == y1 && x < x2)
{
	x++;
}
else if (x == x2 && y < y2)
{
	y++;
}
else if (y == y2 && x > x1)
{
	x--;
}
else if (x == x1 && y > y1)
{
	y--;
}

돌리는 횟수는 테두리 길이를 기준으로 정하면 되겠다

int tmp = r % border_length;

회전 후 배열 a[0][0]의 위치를 찾고 그 위치를 기준으로 배열을 담아보자

int y = starting_point, x = starting_point;
while (tmp)
{
		rotate_y_x(&y, &x, starting_point, starting_point, n - 1 - starting_point, m - 1 - starting_point);
		tmp--;
}

정답 코드

#include<bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
int arr[302][302];
int tmp_arr[302][302];
void rotate_y_x(int& y, int& x, const int y1, const int x1, const int y2, const int x2)
{
	if (y == y1 && x < x2)
	{
		x++;
	}
	else if (x == x2 && y < y2)
	{
		y++;
	}
	else if (y == y2 && x > x1)
	{
		x--;
	}
	else if (x == x1 && y > y1)
	{
		y--;
	}
}

void rotate(const int n, const int m, const int r)
{
	int starting_point = 0;
	int nn = n, mm = m;
	while (nn && mm)
	{
		const int border_length = nn * 2 + (mm - 2) * 2;

		int tmp = r % border_length;
		if (tmp)
		{
			int y = starting_point, x = starting_point;
			while (tmp)
			{
				rotate_y_x(y, x, starting_point, starting_point, n - 1 - starting_point, m - 1 - starting_point);
				tmp--;
			}
			int dy = starting_point, dx = starting_point;
			for (int i = 0; i < border_length; i++)
			{

				arr[dy][dx] = tmp_arr[y][x];
				rotate_y_x(y, x, starting_point, starting_point, n - 1 - starting_point, m - 1 - starting_point);
				rotate_y_x(dy, dx, starting_point, starting_point, n - 1 - starting_point, m - 1 - starting_point);
			}
		}
		starting_point++;
		nn -= 2; mm -= 2;
	}
}
void print_array(const int n, const  int m)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << arr[i][j] << ' ';
		}
		cout << '\n';
	}
}
int main() {
	FAST_IO;
	int n, m, r; cin >> n >> m >> r;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			tmp_arr[i][j] = arr[i][j];
		}
	}

	rotate(n, m, r);

	print_array(n, m);

	return 0;
}

0개의 댓글