본문 바로가기
개발/C, C++

N x N 2차원 배열 뒤집기, 회전하기 (Rotate, Flip 2D Array)

by 피로물든딸기 2022. 11. 27.
반응형

C, C++ 전체 링크

 

2차원 배열의 크기가 N x N인 경우 배열을 뒤집거나 회전하는 코드는 다음과 같다.

#include <stdio.h>

#define SIZE (5)

int map[SIZE][SIZE];

void init(int map[SIZE][SIZE])
{
	int count = 1;
	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			map[i][k] = count++;
}

void printMap(int map[SIZE][SIZE])
{
	for (int i = 0; i < SIZE; i++)
	{
		for (int k = 0; k < SIZE; k++)
			printf("%02d ", map[i][k]);
		putchar('\n');
	}
	putchar('\n');
}

void copyMap(int src[SIZE][SIZE], int dest[SIZE][SIZE])
{
	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			src[i][k] = dest[i][k];
}

void flipUpDown(int map[SIZE][SIZE])
{
	int temp[SIZE][SIZE];

	copyMap(temp, map);

	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			map[i][k] = temp[SIZE - 1 - i][k];
}

void flipLeftRight(int map[SIZE][SIZE])
{
	int temp[SIZE][SIZE];

	copyMap(temp, map);

	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			map[i][k] = temp[i][SIZE - 1 - k];
}

void rotateClockwise90(int map[SIZE][SIZE])
{
	int temp[SIZE][SIZE];

	copyMap(temp, map);

	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			map[i][k] = temp[SIZE - 1 - k][i];
}

void rotateClockwise180(int map[SIZE][SIZE])
{
	int temp[SIZE][SIZE];

	copyMap(temp, map);

	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			map[i][k] = temp[SIZE - 1 - i][SIZE - 1 - k];
}

void rotateClockwise270(int map[SIZE][SIZE])
{
	int temp[SIZE][SIZE];

	copyMap(temp, map);

	for (int i = 0; i < SIZE; i++)
		for (int k = 0; k < SIZE; k++)
			map[i][k] = temp[k][SIZE - 1 - i];
}

int main(void)
{
	printf("Original \n");
	init(map);
	printMap(map);

	printf("Flip Up / Down\n");
	init(map);
	flipUpDown(map);
	printMap(map);

	printf("Flip Left / Right\n");
	init(map);
	flipLeftRight(map);
	printMap(map);

	printf("Rotate Clockwise 90 = CounterClockwise 270\n");
	init(map);
	rotateClockwise90(map);
	printMap(map);

	printf("Rotate Clockwise 180 = CounterClockwise 180\n");
	init(map);
	rotateClockwise180(map);
	printMap(map);

	printf("Rotate Clockwise 270 = CounterClockwise 90\n");
	init(map);
	rotateClockwise270(map);
	printMap(map);

	return 0;
}

 

원본 배열은 다음과 같다.

Original 
01 02 03 04 05 
06 07 08 09 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25

 

각 함수의 결과는 다음과 같다.

Flip Up / Down
21 22 23 24 25 
16 17 18 19 20 
11 12 13 14 15 
06 07 08 09 10 
01 02 03 04 05 

Flip Left / Right
05 04 03 02 01 
10 09 08 07 06 
15 14 13 12 11 
20 19 18 17 16 
25 24 23 22 21 

Rotate Clockwise 90 = CounterClockwise 270
21 16 11 06 01 
22 17 12 07 02 
23 18 13 08 03 
24 19 14 09 04 
25 20 15 10 05 

Rotate Clockwise 180 = CounterClockwise 180
25 24 23 22 21 
20 19 18 17 16 
15 14 13 12 11 
10 09 08 07 06 
05 04 03 02 01 

Rotate Clockwise 270 = CounterClockwise 90
05 10 15 20 25 
04 09 14 19 24 
03 08 13 18 23 
02 07 12 17 22 
01 06 11 16 21

 

SIZE가 6(= 짝수)인 경우는 아래와 같은 결과를 얻는다.

Original 
01 02 03 04 05 06 
07 08 09 10 11 12 
13 14 15 16 17 18 
19 20 21 22 23 24 
25 26 27 28 29 30 
31 32 33 34 35 36 

Flip Up / Down
31 32 33 34 35 36 
25 26 27 28 29 30 
19 20 21 22 23 24 
13 14 15 16 17 18 
07 08 09 10 11 12 
01 02 03 04 05 06 

Flip Left / Right
06 05 04 03 02 01 
12 11 10 09 08 07 
18 17 16 15 14 13 
24 23 22 21 20 19 
30 29 28 27 26 25 
36 35 34 33 32 31 

Rotate Clockwise 90 = CounterClockwise 270
31 25 19 13 07 01 
32 26 20 14 08 02 
33 27 21 15 09 03 
34 28 22 16 10 04 
35 29 23 17 11 05 
36 30 24 18 12 06 

Rotate Clockwise 180 = CounterClockwise 180
36 35 34 33 32 31 
30 29 28 27 26 25 
24 23 22 21 20 19 
18 17 16 15 14 13 
12 11 10 09 08 07 
06 05 04 03 02 01 

Rotate Clockwise 270 = CounterClockwise 90
06 12 18 24 30 36 
05 11 17 23 29 35 
04 10 16 22 28 34 
03 09 15 21 27 33 
02 08 14 20 26 32 
01 07 13 19 25 31
반응형

댓글