https://programmers.co.kr/learn/courses/30/lessons/17679#
나의 코드
실패한 코드
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
vector<pair<int, int>>check = { {0,0},{0,1},{1,0},{1,1} };
stack<pair<int, int>> s;
void check4(int m, int n, vector<string> &board) {
stack<pair<int, int>> tmp;
for (int i = 0; i < m-1; i++) {
for (int k = 0; k < n-1; k++) {
bool tag = true;
char c = board[i][k];
if (c != '.') {
for (int t = 0; t < 4; t++) {
if (c != board[i + check[t].first][k + check[t].second]) {
tag = false;
break;
}
}
if (tag) {
s.push({ i,k });
tmp.push({ i,k });
}
}
}
}
while (!tmp.empty()) {
int i = tmp.top().first, k = tmp.top().second;
tmp.pop();
for (int t = 0; t < 4; t++) {
board[i + check[t].first][k + check[t].second] = '.';
}
}
}
int solution(int m, int n, vector<string> board) {
int answer = 0;
check4(m,n,board);
while (!s.empty()) {
while (!s.empty()) {
int x = s.top().first, y = s.top().second;
s.pop();
for (int i = 0; i < 2; i++) {
int num = 0;
for (int k = x; k < m && board[k][y + i] == '.'; k++, num++);
for (int k = x - 1; k >= 0 && board[k][y+i] != '.'; k--) {
board[k + num][y + i] = board[k][y + i];
board[k][y + i] = '.';
}
}
}
check4(m, n, board);
}
for (int i = 0; i < m; i++) {
for (int k = 0; k < n; k++) {
if (board[i][k] == '.') {
answer++;
}
}
}
return answer;
}
테스트 6번만 돌아가지 않아 애를 먹었는데
테스트 6번에서 내 코드가 빈공간을 내려갈때 빈공간이 아닌 칸을 덮어버리는 문제가 있는것 같아 이렇게 수정했다.
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
vector<pair<int, int>>check = { {0,0},{0,1},{1,0},{1,1} };
stack<pair<int, int>> s;
void check4(int m, int n, vector<string> &board) {
stack<pair<int, int>> tmp;
for (int i = 0; i < m-1; i++) {
for (int k = 0; k < n-1; k++) {
bool tag = true;
char c = board[i][k];
if (c != '.') {
for (int t = 0; t < 4; t++) {
if (c != board[i + check[t].first][k + check[t].second]) {
tag = false;
break;
}
}
if (tag) {
s.push({ i,k });
tmp.push({ i,k });
}
}
}
}
while (!tmp.empty()) {
int i = tmp.top().first, k = tmp.top().second;
tmp.pop();
for (int t = 0; t < 4; t++) {
board[i + check[t].first][k + check[t].second] = '.';
}
}
}
int solution(int m, int n, vector<string> board) {
int answer = 0;
check4(m,n,board);
while (!s.empty()) {
while (!s.empty()) {
int x = s.top().first, y = s.top().second;
s.pop();
for (int i = 0; i < 2; i++) {
int num = 0;
for (int k = x; k < m && board[k][y + i] == '.'; k++, num++);
for (int k = x - 1; k >= 0 && board[k][y+i] != '.'; k--) {
if (board[k + num][y + i] == '.') {
board[k + num][y + i] = board[k][y + i];
board[k][y + i] = '.';
}
}
}
}
check4(m, n, board);
}
for (int i = 0; i < m; i++) {
for (int k = 0; k < n; k++) {
if (board[i][k] == '.') {
answer++;
}
}
}
return answer;
}
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "배달" (0) | 2022.04.30 |
---|---|
(c++) 백준 "2304. 창고 다각형" (0) | 2022.04.29 |
(c++) 프로그래머스 "[3차] 압축" (0) | 2022.04.28 |
(c++) 백준 "2559. 수열" (0) | 2022.04.27 |
(c++) 프로그래머스 "숫자의 표현" (0) | 2022.04.27 |