티스토리 뷰

출처 :https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5V61LqAf8DFAWu


#include <iostream>

#include <algorithm>
using namespace std;
 
int N, M, K, result, MAX_K, num;
int map[20][20];
 
void square(int i, int j, int depth)
{
    int starti = i; int startj = j;
    while (1)
    {
        if (i >= 0 && j >= 0 && i < N && j < N)
        {
            if (map[i][j]) num++;
        }
        i--;j++;
        if (i == starti - depth && j == startj + depth) break;
    }
    while (1)
    {
        if (i >= 0 && j >= 0 && i < N && j < N)
        {
            if (map[i][j]) num++;
        }
        i++;j++;
        if (i == starti && j == startj + 2*depth) break;
    }
    while (1)
    {
        if (i >= 0 && j >= 0 && i < N && j < N)
        {
            if (map[i][j]) num++;
        }
        i++;j--;
        if (i == starti + depth && j == startj + depth) break;
    }
    while (1)
    {
        if (i >= 0 && j >= 0 && i < N && j < N)
        {
            if (map[i][j]) num++;
        }
        i--;j--;
        if (i == starti && j == startj) break;
    }
 
}
 
void dfs(int i, int j, int depth)
{
    while (depth <= MAX_K)
    {
        int starti = i; int startj = j - depth;
        square(starti, startj, depth);
        depth++;
        if ((num*M) - ((depth*depth) + (depth - 1)*(depth - 1)) >= 0) result = max(result, num);
    }
}
 
 
int main()
{
    int T; int tc = 1;
    cin >> T;
    while (T--)
    {  
        cin >> N >> M; result = 0; K = 0;
        for (int i = 0;i < N;i++) for (int j = 0;j < N; j++)
        {
            cin >> map[i][j]; K += map[i][j];
        }
        for (int i = 50; i > 0; i--) if (((i * i) + ((i - 1) * (i - 1))) <= K*M) { MAX_K = i; break; }
         
        for (int i = 0; i < N; i++) for (int j = 0; j < N; j++)
        {
            if (map[i][j]) { num = 1; dfs(i, j, 1); }
            else { num = 0; dfs(i, j, 1); }
        }
        if (result == 0) result = 1;
 
        cout <<"#" << tc++ << " " << result << endl;
    }
         
    return 0;
}


'알고리즘 > SWEA' 카테고리의 다른 글

[SWEA 4789] 성공적인 공연 기획  (0) 2018.07.26
[SWEA 4747] 사막에서 만난 지니  (0) 2018.07.26
[SWEA 4012] 요리사  (0) 2018.07.23
[SWEA 2115] 벌꿀채취  (0) 2018.07.23
[SWEA 4008] 숫자 만들기  (0) 2018.07.23
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/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
글 보관함