본문 바로가기
알고리즘/[ADV] 삼성 SW 역량 테스트 A형

[코드트리] 2차원 테트리스 (삼성 SW 역량테스트 2020 상반기 오전 1번)

by 피로물든딸기 2024. 6. 8.
반응형

삼성 A형 전체 링크

 

https://www.codetree.ai/training-field/frequent-problems/problems/tetris-2d

 

2차원 테트리스 문제 풀이BOJ 20061 : 모노미노도미노 2와 같다.

#include <stdio.h>

#define START (4)
#define END (9)

int T;
int N;
int MAP[11][11];

void output()
{
	for (int r = 0; r < 11; r++)
	{
		for (int c = 0; c < 11; c++)
			printf("%d ", MAP[r][c]);
		putchar('\n');
	}
	putchar('\n');
}

void moveBlue(int t, int sr, int sc)
{
	int c;

	switch (t)
	{
	case 1:

		for (c = sc; ; c++)
			if (MAP[sr][c + 1] != 0) break;

		MAP[sr][c] = 1;

		break;
	case 2:

		for (c = sc; ; c++)
			if (MAP[sr][c + 2] != 0) break;

		MAP[sr][c] = MAP[sr][c + 1] = 1;

		break;
	case 3:

		for (c = sc; ; c++)
		{
			if (MAP[sr][c + 1] != 0) break;
			if (MAP[sr + 1][c + 1] != 0) break;
		}

		MAP[sr][c] = MAP[sr + 1][c] = 1;

		break;
	default:
		break;
	}
}

void moveGreen(int t, int sr, int sc)
{
	int r;

	switch (t)
	{
	case 1:

		for (r = sr; ; r++)
			if (MAP[r + 1][sc] != 0) break;

		MAP[r][sc] = 1;

		break;
	case 2:

		for (r = sr; ; r++)
		{
			if (MAP[r + 1][sc] != 0) break;
			if (MAP[r + 1][sc + 1] != 0) break;
		}

		MAP[r][sc] = MAP[r][sc + 1] = 1;
		break;
	case 3:

		for (r = sr; ; r++)
			if (MAP[r + 2][sc] != 0) break;

		MAP[r][sc] = MAP[r + 1][sc] = 1;

		break;
	default:
		break;
	}
}

void deleteBlue(int column)
{
	for (int c = column; c > START; c--)
		for (int r = 0; r < 4; r++)
			MAP[r][c] = MAP[r][c - 1];

	MAP[0][START] = MAP[1][START] = MAP[2][START] = MAP[3][START] = 0;
}

void deleteGreen(int row)
{
	for (int r = row; r > START; r--)
		for (int c = 0; c < 4; c++)
			MAP[r][c] = MAP[r - 1][c];

	MAP[START][0] = MAP[START][1] = MAP[START][2] = MAP[START][3] = 0;
}

int getScoreBlue()
{
	for (int c = START; c <= END; c++)
	{
		int cnt = 0;

		for (int r = 0; r < 4; r++) cnt += MAP[r][c];

		if (cnt == 4)
		{
			deleteBlue(c);
			return 1;
		}
	}

	return 0;
}

int getScoreGreen()
{
	for (int r = START; r <= END; r++)
	{
		int cnt = 0;
		for (int c = 0; c < 4; c++) cnt += MAP[r][c];

		if (cnt == 4)
		{
			deleteGreen(r);
			return 1;
		}
	}

	return 0;
}

int checkBlue()
{
	for (int r = 0; r < 4; r++)
		if (MAP[r][START + 1]) return 1;

	return 0;
}

int checkGreen()
{
	for (int c = 0; c < 4; c++)
		if (MAP[START + 1][c]) return 1;

	return 0;
}

int main(void)
{
	MAP[0][10] = MAP[1][10] = MAP[2][10] = MAP[3][10] = -1;
	MAP[10][0] = MAP[10][1] = MAP[10][2] = MAP[10][3] = -1;

	// scanf("%d", &T);
	T = 1;
	for (int tc = 1; tc <= T; tc++)
	{
		scanf("%d", &N);

		int score = 0;
		for (int i = 0; i < N; i++)
		{
			int t, r, c;

			scanf("%d %d %d", &t, &r, &c);

			/* block 이동 */
			moveBlue(t, r, c);
			moveGreen(t, r, c);

			/* score 계산 */
			score += getScoreBlue();
			score += getScoreBlue();

			score += getScoreGreen();
			score += getScoreGreen();

			/* 특별한 칸 처리 */
			if (checkBlue()) deleteBlue(END);
			if (checkBlue()) deleteBlue(END);

			if (checkGreen()) deleteGreen(END);
			if (checkGreen()) deleteGreen(END);
		}

		int blockCount = 0;

		for (int r = START; r <= END; r++)
			for (int c = 0; c < 4; c++) blockCount += MAP[r][c];

		for (int c = START; c <= END; c++)
			for (int r = 0; r < 4; r++) blockCount += MAP[r][c];

		printf("%d\n%d\n", score, blockCount);
	}

	return 0;
}
반응형

댓글