[Programmers] 블록 게임

Time Lapse :3hour 12min 32sec

solution.cpp

1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <string>
#include <vector>
#include <stdio.h>
#include <memory.h>
#include <queue>
//3:12:32
using namespace std;
int n;
vector<vector<int>> Board;

bool canFill(int row,int col){
for(int i=0;i<row;i++)
if(Board[i][col]!=0) return false;

return true;
}

bool find(int row, int col, int h, int w){
int zero = 0;
int block = -1;
for(int i=row; i<row + h; i++){
for(int j=col; j<col+w;j++){
if(Board[i][j]==0){
if(!canFill(i,j)) return false;
if(++zero>2) return false;
}
else{
if(block!=-1&&Board[i][j]!=block) return false;
block = Board[i][j];
}
}
}
for(int i=row; i<row+h;i++){
for(int j=col;j<col+w;j++){
Board[i][j]=0;
}
}
return true;
}

int solution(vector<vector<int>> board) {
Board=board;
n=board.size();
int answer = 0;
int cnt = 0;
do{
cnt = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i<=n-3&&j<=n-2&&find(i,j,3,2)) cnt++;
else if(i<=n-2&&j<=n-3&&find(i,j,2,3)) cnt++;
}
}
answer+=cnt;
}while(cnt);

return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/blockGame/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.