티스토리 뷰

입력받은 2차원 배열 전체를 돌면서, 모든 (i,j) 쌍에서 가능한 회문의 최대 길이를 구하면 그 중 최대값이 답이 됩니다.


C++ 소스 코드

 

#include <iostream>
#include <string>
#define MAX 100
using namespace std;
string map[MAX];
int check(int x, int y){
    for(int k=MAX;k>=2;k--){
        int width = k,height = k;
        for(int t = 0;t<=k/2;t++){
            if((y+k-1)< MAX) {
                if(map[x][y+t]!= map[x][k-t+y-1]) width = 1;
            }else width = 1;
            if ((x+k-1)<MAX){
                if (map[x+t][y]!= map[k-t+x-1][y]) height = 1;
            }else height = 1;
        }
        if (width > 1) return width;
        if (height > 1) return height;
    }
    return 1;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc;
    for(int test_case = 1; test_case<=10;test_case++){
        cin >> tc;
        int ans = 1;
        for(int i=0;i<MAX;i++){
            cin >> map[i];
        }
        for(int i=0;i<MAX;i++){
            for(int j=0;j<MAX;j++){
                int cnt = check(i,j);
                if (cnt > ans) ans = cnt;
            }
        }
        cout <<"#" <<test_case <<" " << ans <<endl;
    }
    return 0;
}

'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글

프로그래머스) Lv4 - 스티커 모으기(2)  (0) 2020.08.25
SWEA) 1209 - Sum  (0) 2020.08.15
BOJ) 5427 - 불  (0) 2020.08.09
프로그래머스) Lv4 - 게임 맵 최단거리  (0) 2020.08.03
BOJ) 1966 - 프린터 큐  (0) 2020.08.02
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/12   »
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
글 보관함