Programmers 17679 [1차] 프렌즈4블록 [2018 KAKAO BLIND RECRUITMENT]

프렌즈4블록

https://school.programmers.co.kr/learn/courses/30/lessons/17679

블라인드 공채를 통과한 신입 사원 라이언은 신규 게임 개발 업무를 맡게 되었다.

이번에 출시할 게임 제목은 “프렌즈4블록”.

같은 모양의 카카오프렌즈 블록이 2×2 형태로 4개가 붙어있을 경우 사라지면서 점수를 얻는 게임이다.

board map

만약 판이 위와 같이 주어질 경우, 라이언이 2×2로 배치된 7개 블록과 콘이 2×2로 배치된 4개 블록이 지워진다.

같은 블록은 여러 2×2에 포함될 수 있으며, 지워지는 조건에 만족하는 2×2 모양이 여러 개 있다면 한꺼번에 지워진다.

board map

블록이 지워진 후에 위에 있는 블록이 아래로 떨어져 빈 공간을 채우게 된다.

board map

만약 빈 공간을 채운 후에 다시 2×2 형태로 같은 모양의 블록이 모이면 다시 지워지고 떨어지고를 반복하게 된다.


board map

위 초기 배치를 문자로 표시하면 아래와 같다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
TTTANT
RRFACC
RRRFCC
TRRRAA
TTMMMF
TMMTTJ
TTTANT RRFACC RRRFCC TRRRAA TTMMMF TMMTTJ
TTTANT
RRFACC
RRRFCC
TRRRAA
TTMMMF
TMMTTJ

각 문자는 라이언(R), 무지(M), 어피치(A), 프로도(F), 네오(N), 튜브(T), 제이지(J), 콘(C)을 의미한다

입력으로 블록의 첫 배치가 주어졌을 때, 지워지는 블록은 모두 몇 개인지 판단하는 프로그램을 제작하라.

입력 형식

  • 입력으로 판의 높이 m, 폭 n과 판의 배치 정보 board가 들어온다.
  • 2 ≦ nm ≦ 30
  • board는 길이 n인 문자열 m개의 배열로 주어진다. 블록을 나타내는 문자는 대문자 A에서 Z가 사용된다.

출력 형식

입력으로 주어진 판 정보를 가지고 몇 개의 블록이 지워질지 출력하라.

입출력 예제

mnboardanswer
45[“CCBDE”, “AAADE”, “AAABF”, “CCBBF”]14
66[“TTTANT”, “RRFACC”, “RRRFCC”, “TRRRAA”, “TTMMMF”, “TMMTTJ”]15

예제에 대한 설명

  • 입출력 예제 1의 경우, 첫 번째에는 A 블록 6개가 지워지고, 두 번째에는 B 블록 4개와 C 블록 4개가 지워져, 모두 14개의 블록이 지워진다.
  • 입출력 예제 2는 본문 설명에 있는 그림을 옮긴 것이다. 11개와 4개의 블록이 차례로 지워지며, 모두 15개의 블록이 지워진다.

통과된 코드(1)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <string>
#include <vector>
using namespace std;
constexpr int Max = 30;
int N, M;
char map[Max][Max];
bool mapCheck[Max][Max];
void CheckRectangle(int x, int y, char c)
{
bool check = false;
if (x - 1 >= 0 && y - 1 >= 0 ) { // 좌 상단 확인
if (map[x - 1][y - 1] == c && map[x][y - 1] == c && map[x - 1][y] == c) {
check = true;
mapCheck[x - 1][y - 1] = true;
mapCheck[x][y - 1] = true;
mapCheck[x - 1][y] = true;
}
}
if (x - 1 >= 0 && y + 1 < N) { // 우 상단 확인
if (map[x - 1][y] == c && map[x][y + 1] == c && map[x - 1][y + 1] == c) {
check = true;
mapCheck[x - 1][y] = true;
mapCheck[x][y + 1] = true;
mapCheck[x - 1][y + 1] = true;
}
}
if (x + 1 < M && y + 1 < N) { // 우 하단 확인
if (map[x][y + 1] == c && map[x + 1][y + 1] == c && map[x + 1][y] == c) {
check = true;
mapCheck[x][y + 1] = true;
mapCheck[x + 1][y + 1] = true;
mapCheck[x + 1][y] = true;
}
}
if (x + 1 < M && y - 1 >= 0) { // 좌 하단 확인
if (map[x + 1][y - 1] == c && map[x + 1][y] == c && map[x][y - 1] == c) {
check = true;
mapCheck[x + 1][y - 1] = true;
mapCheck[x + 1][y] = true;
mapCheck[x][y - 1] = true;
}
}
if (check) mapCheck[x][y] = true;
}
int solution(int m, int n, vector<string> board) {
M = m;
N = n;
int answer = 0;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].length(); j++) {
map[i][j] = board[i][j];
}
}
while (true) {
int cnt = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] != ' ') CheckRectangle(i, j, map[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mapCheck[i][j]) {
map[i][j] = ' ';
mapCheck[i][j] = false;
cnt++;
}
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
if (map[i][j] == ' ') {
for (int k = i; k >= 0; k--) {
if (map[k][j] != ' ') {
map[i][j] = map[k][j];
map[k][j] = ' ';
break;
}
}
}
}
}
if (cnt == 0) break;
else answer += cnt;
}
return answer;
}
#include <string> #include <vector> using namespace std; constexpr int Max = 30; int N, M; char map[Max][Max]; bool mapCheck[Max][Max]; void CheckRectangle(int x, int y, char c) { bool check = false; if (x - 1 >= 0 && y - 1 >= 0 ) { // 좌 상단 확인 if (map[x - 1][y - 1] == c && map[x][y - 1] == c && map[x - 1][y] == c) { check = true; mapCheck[x - 1][y - 1] = true; mapCheck[x][y - 1] = true; mapCheck[x - 1][y] = true; } } if (x - 1 >= 0 && y + 1 < N) { // 우 상단 확인 if (map[x - 1][y] == c && map[x][y + 1] == c && map[x - 1][y + 1] == c) { check = true; mapCheck[x - 1][y] = true; mapCheck[x][y + 1] = true; mapCheck[x - 1][y + 1] = true; } } if (x + 1 < M && y + 1 < N) { // 우 하단 확인 if (map[x][y + 1] == c && map[x + 1][y + 1] == c && map[x + 1][y] == c) { check = true; mapCheck[x][y + 1] = true; mapCheck[x + 1][y + 1] = true; mapCheck[x + 1][y] = true; } } if (x + 1 < M && y - 1 >= 0) { // 좌 하단 확인 if (map[x + 1][y - 1] == c && map[x + 1][y] == c && map[x][y - 1] == c) { check = true; mapCheck[x + 1][y - 1] = true; mapCheck[x + 1][y] = true; mapCheck[x][y - 1] = true; } } if (check) mapCheck[x][y] = true; } int solution(int m, int n, vector<string> board) { M = m; N = n; int answer = 0; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].length(); j++) { map[i][j] = board[i][j]; } } while (true) { int cnt = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (map[i][j] != ' ') CheckRectangle(i, j, map[i][j]); } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (mapCheck[i][j]) { map[i][j] = ' '; mapCheck[i][j] = false; cnt++; } } } for (int i = m - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { if (map[i][j] == ' ') { for (int k = i; k >= 0; k--) { if (map[k][j] != ' ') { map[i][j] = map[k][j]; map[k][j] = ' '; break; } } } } } if (cnt == 0) break; else answer += cnt; } return answer; }
#include <string>
#include <vector>

using namespace std;

constexpr int Max = 30;

int N, M;

char map[Max][Max];
bool mapCheck[Max][Max];

void CheckRectangle(int x, int y, char c)
{
	bool check = false;
	if (x - 1 >= 0 && y - 1 >= 0 ) { // 좌 상단 확인

		if (map[x - 1][y - 1] == c && map[x][y - 1] == c && map[x - 1][y] == c) {
			check = true;
			mapCheck[x - 1][y - 1] = true;
			mapCheck[x][y - 1] = true;
			mapCheck[x - 1][y] = true;
		}
	}

	if (x - 1 >= 0 && y + 1 < N) { // 우 상단 확인

		if (map[x - 1][y] == c && map[x][y + 1] == c && map[x - 1][y + 1] == c) {
			check = true;
			mapCheck[x - 1][y] = true;
			mapCheck[x][y + 1] = true;
			mapCheck[x - 1][y + 1] = true;
		}
	}

	if (x + 1 < M && y + 1 < N) { // 우 하단 확인

		if (map[x][y + 1] == c && map[x + 1][y + 1] == c && map[x + 1][y] == c) {
			check = true;
			mapCheck[x][y + 1] = true;
			mapCheck[x + 1][y + 1] = true;
			mapCheck[x + 1][y] = true;
		}
	}

	if (x + 1 < M && y - 1 >= 0) { // 좌 하단 확인

		if (map[x + 1][y - 1] == c && map[x + 1][y] == c && map[x][y - 1] == c) {
			check = true;
			mapCheck[x + 1][y - 1] = true;
			mapCheck[x + 1][y] = true;
			mapCheck[x][y - 1] = true;
		}
	}

	if (check) mapCheck[x][y] = true;


}


int solution(int m, int n, vector<string> board) {

	M = m;
	N = n;

    int answer = 0;

	for (int i = 0; i < board.size(); i++) {
		for (int j = 0; j < board[i].length(); j++) {
			map[i][j] = board[i][j];
		}
	}
	

	while (true) {
		int cnt = 0;

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (map[i][j] != ' ') CheckRectangle(i, j, map[i][j]);
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (mapCheck[i][j]) {
					map[i][j] = ' ';
					mapCheck[i][j] = false;
					cnt++;		
				}
			}
		}

		for (int i = m - 1; i >= 0; i--) {
			for (int j = 0; j < n; j++) {
				if (map[i][j] == ' ') {
					for (int k = i; k >= 0; k--) {
						if (map[k][j] != ' ') {
							map[i][j] = map[k][j];
							map[k][j] = ' ';
							break;
						}
					}
				}
			}
		}

		if (cnt == 0) break;
		else answer += cnt;
	}

    return answer;
}

통과된 코드(2)

생각해보니 모든 사분면을 확인할 필요가 없었다.

더 유연한 생각이 필요한 것 같다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <string>
#include <vector>
using namespace std;
constexpr int Max = 30;
int N, M;
char map[Max][Max];
bool mapCheck[Max][Max];
void CheckRectangle(int x, int y, char c)
{
bool check = false;
if (x + 1 < M && y + 1 < N) {
if (map[x][y + 1] == c && map[x + 1][y + 1] == c && map[x + 1][y] == c) {
check = true;
mapCheck[x][y + 1] = true;
mapCheck[x + 1][y + 1] = true;
mapCheck[x + 1][y] = true;
}
}
if (check) mapCheck[x][y] = true;
}
int solution(int m, int n, vector<string> board) {
M = m;
N = n;
int answer = 0;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].length(); j++) {
map[i][j] = board[i][j];
}
}
while (true) {
int cnt = 0;
for (int i = 0; i < m-1; i++) {
for (int j = 0; j < n-1; j++) {
if (map[i][j] != ' ') CheckRectangle(i, j, map[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mapCheck[i][j]) { // 블록을 터뜨림
map[i][j] = ' ';
mapCheck[i][j] = false;
cnt++;
}
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
if (map[i][j] == ' ') {
for (int k = i; k >= 0; k--) {
if (map[k][j] != ' ') {
map[i][j] = map[k][j];
map[k][j] = ' ';
break;
}
}
}
}
}
if (cnt == 0) break;
else answer += cnt;
}
return answer;
}
#include <string> #include <vector> using namespace std; constexpr int Max = 30; int N, M; char map[Max][Max]; bool mapCheck[Max][Max]; void CheckRectangle(int x, int y, char c) { bool check = false; if (x + 1 < M && y + 1 < N) { if (map[x][y + 1] == c && map[x + 1][y + 1] == c && map[x + 1][y] == c) { check = true; mapCheck[x][y + 1] = true; mapCheck[x + 1][y + 1] = true; mapCheck[x + 1][y] = true; } } if (check) mapCheck[x][y] = true; } int solution(int m, int n, vector<string> board) { M = m; N = n; int answer = 0; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].length(); j++) { map[i][j] = board[i][j]; } } while (true) { int cnt = 0; for (int i = 0; i < m-1; i++) { for (int j = 0; j < n-1; j++) { if (map[i][j] != ' ') CheckRectangle(i, j, map[i][j]); } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (mapCheck[i][j]) { // 블록을 터뜨림 map[i][j] = ' '; mapCheck[i][j] = false; cnt++; } } } for (int i = m - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { if (map[i][j] == ' ') { for (int k = i; k >= 0; k--) { if (map[k][j] != ' ') { map[i][j] = map[k][j]; map[k][j] = ' '; break; } } } } } if (cnt == 0) break; else answer += cnt; } return answer; }
#include <string>
#include <vector>

using namespace std;

constexpr int Max = 30;

int N, M;

char map[Max][Max];
bool mapCheck[Max][Max];


void CheckRectangle(int x, int y, char c)
{
    bool check = false;

    if (x + 1 < M && y + 1 < N) {

        if (map[x][y + 1] == c && map[x + 1][y + 1] == c && map[x + 1][y] == c) {
            check = true;
            mapCheck[x][y + 1] = true;
            mapCheck[x + 1][y + 1] = true;
            mapCheck[x + 1][y] = true;
        }
    }

    if (check) mapCheck[x][y] = true;


}


int solution(int m, int n, vector<string> board) {

    M = m;
    N = n;

    int answer = 0;

    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[i].length(); j++) {
            map[i][j] = board[i][j];
        }
    }


    while (true) {
        int cnt = 0;

        for (int i = 0; i < m-1; i++) {
            for (int j = 0; j < n-1; j++) {
                if (map[i][j] != ' ') CheckRectangle(i, j, map[i][j]);
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (mapCheck[i][j]) { // 블록을 터뜨림
                    map[i][j] = ' ';
                    mapCheck[i][j] = false;
                    cnt++;
                }
            }
        }

        for (int i = m - 1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (map[i][j] == ' ') {
                    for (int k = i; k >= 0; k--) {
                        if (map[k][j] != ' ') {
                            map[i][j] = map[k][j];
                            map[k][j] = ' ';
                            break;
                        }
                    }
                }
            }
        }

        if (cnt == 0) break;
        else answer += cnt;
    }

    return answer;
}

처음 작성한 코드보다 속도가 크게 개선되었다. (특히 테스트 5번)

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다