이 문제가 DP라는 것을 왜 깨닫지 못했는가...

 

 

 

 

 

d[i][j] 에서 sq[i][j] == 0이면 0이고

              sq[i][j] == 1이면 min(d[i-1][j], d[i][j-1], d[i-1][j-1]) +1 

...

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define pii pair<intint>
using namespace std;
 
int d[1000][1000= { 0 };
int solution(vector<vector<int>> sq)
{
    int ans = 0;
    for (int i = 0; i < sq.size(); i++)
    {
        d[i][0= sq[i][0];
        ans = max(ans, d[i][0]);
    }
    for (int i = 0; i < sq[0].size(); i++)
    {
        d[0][i] = sq[0][i];
        ans = max(ans, d[0][i]);
    }
    
    for (int i = 1; i < sq.size(); i++)
    {
        for (int j = 1; j < sq[i].size(); j++)
        {
            if (sq[i][j] == 0)
                d[i][j] = 0;
            else
                d[i][j] = min(d[i - 1][j], min(d[i][j - 1], d[i - 1][j - 1])) + 1;
 
            ans = max(ans, d[i][j]);
        }
    }
 
 
    return ans * ans;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
 
    vector<vector<int>> sq = { {0,1,1,1},{1,1,1,1},{1,1,1,1},{0,0,1,0} };
 
    cout << solution(sq);
 
}
 
cs

 

'뚝배기 터진 문제_programmers' 카테고리의 다른 글

lv3 N으로 표현.  (0) 2020.09.05
lv2 후보키  (0) 2020.09.05
lv2 조이스틱  (0) 2020.08.28
LV2_문자열 압축  (0) 2020.08.26
LV2_큰 수 만들기  (0) 2020.08.25

+ Recent posts