Algorithm, Data structure/Solved Algorithmic Problem

BAEKJOON 1051 - 숫자 정사각형

JaykayChoi 2016. 11. 12. 22:58

문제: https://www.acmicpc.net/problem/1051



특별한 기법없이 완전탐색으로 풀 수 있었던 문제네요. 이미 구해놓은 최대값보다 큰 길이만 검사하는 가지치기 방법을 사용하면 시간 복잡도 문제 없이 풀 수 있는 문제인 것 같습니다.



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
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include <algorithm>
      
#include <string>
using namespace std;
 
int height, width;
int map[51][51];
 
int getMaxLength()
{
    int ret = 0;
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            int num = map[h][w];
            int maxLength = height - h - 1;
            if (width - w - < maxLength)
                maxLength = width - w - 1;
            if (maxLength < ret)
                continue;
            for (int length = ret; length <= maxLength; length++)
            {
                if (map[h][w + length] == num && map[h + length][w + length] == num && map[h + length][w] == num)
                    ret = length;
            }
        }
    }
    return ret;
}
 
int main()
{
    cin >> height >> width;
 
    for (int h = 0; h < height; h++)
    {
        string str;
        cin >> str;
        for (int w = 0; w < width; w++)
            map[h][w] = str[w] - '0';
    }
    int ret = getMaxLength() + 1;
    cout << ret * ret << endl;
 
    return 0;
}
 
 
cs


'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글

BAEKJOON 2675 - Repeating Characters  (0) 2016.11.20
BAEKJOON 1052 - 물병  (0) 2016.11.13
BAEKJOON 1038 - 감소하는 수  (0) 2016.11.06
BAEKJOON 1037 - 약수  (0) 2016.10.31
BAEKJOON 1029 - 그림 교환  (0) 2016.10.08