티스토리 뷰

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



#include <iostream>

#include <algorithm>
using namespace std;
 
#define MAX_N 12
 
int i, j, N, T, t;
int a[MAX_N][MAX_N],a2[MAX_N][MAX_N], result1, result2, d1, d2;
 
void input()
{
    cin >> N;
    for (i = 0;i < N;i++) for(j=0;j<N;j++) cin >> a[i][j];
 
    for (i = 0;i < N;i++) for (j = 0;j < N;j++) a2[i][j] = a[i][j];
}
 
void cal1()
{  
    int l, r, u, d, count, wh; d1 = 0;
    for (i = 1;i <N-1;i++)
    {
        for (j = 1;j<N-1;j++)
        {
            count = -1; wh = -1; result1 = 0;
            if (a[i][j] == 1)
            {
                //left
                for (l = j-1;l >= 0;l--)
                {
                    if (a[i][l] > 0) { count = count; break; }
                    else { if (l == 0) { count = j; wh = 1; } }
                }
                //right
                for (r = j+1;r <=N-1;r++)
                {
                    if (a[i][r] >0) { count = count; break; }
                    else
                    {
                        if (r == N - 1) {
                            if (count == -1 || count >= N - 1 - j)
                            {
                                count = N - 1 - j; wh = 2;
                            }
                        }
                    }
                }
                //up
                for (u = i-1;u >= 0;u--)
                {
                    if (a[u][j] >0) { count = count; break; }
                    else
                    {
                        if (u == 0) {
                            if (count == -1 || count >= i)
                            {
                                count = i; wh = 3;
                            }
                        }
                    }
                }
                //down
                for (d = i+1;d <=N-1;d++)
                {
                    if (a[d][j] >0) { count = count; break; }
                    else
                    {
                        if (d == N - 1) {
                            if (count == -1 || count >= N - 1 - i)
                            {
                                count = N - 1 - i; wh = 4;
                            }
                        }
                    }
                }
                switch (wh)
                {
                case 1:
                    for (l = 0;a[i][l] < 1;l++) a[i][l] = 2;
                    d1 += 1;
                    break;
                case 2:
                    for (r = N-1;a[i][r]<1;r--) a[i][r] = 2;
                    d1 += 1;
                    break;
                case 3:
                    for (u = 0;a[u][j]<1;u++) a[u][j] = 2;
                    d1 += 1;
                    break;
                case 4:
                    for (d = N-1;a[d][j]<1;d--) a[d][j] = 2;
                    d1 += 1;
                    break;
                case -1:
                    break;
                }
 
            }
        }
    }
 
    for (i = 0;i < N;i++) for (j = 0;j < N;j++)
        if (a[i][j] == 2) result1 += 1;
}
void cal2()
{  
    int l, r, u, d, count, wh; d2 = 0;
    for (i = N-2;i > 0;i--)
    {
        for (j = N-2;j > 0;j--)
        {
            count = -1; wh = -1; result2 = 0;
            if (a2[i][j] == 1)
            {
                //left
                for (l = j-1;l >= 0;l--)
                {
                    if (a2[i][l] > 0) { count = count; break; }
                    else { if (l == 0) { count = j; wh = 1; } }
                }
                //right
                for (r = j+1;r <=N-1;r++)
                {
                    if (a2[i][r] >0) { count = count; break; }
                    else
                    {
                        if (r == N - 1) {
                            if (count == -1 || count >= N - 1 - j)
                            {
                                count = N - 1 - j; wh = 2;
                            }
                        }
                    }
                }
                //up
                for (u = i-1;u >= 0;u--)
                {
                    if (a2[u][j] >0) { count = count; break; }
                    else
                    {
                        if (u == 0) {
                            if (count == -1 || count >= i)
                            {
                                count = i; wh = 3;
                            }
                        }
                    }
                }
                //down
                for (d = i+1;d <=N-1;d++)
                {
                    if (a2[d][j] >0) { count = count; break; }
                    else
                    {
                        if (d == N - 1) {
                            if (count == -1 || count >= N - 1 - i)
                            {
                                count = N - 1 - i; wh = 4;
                            }
                        }
                    }
                }
                switch (wh)
                {
                case 1:
                    for (l = 0;a2[i][l] < 1;l++) a2[i][l] = 2;
                    d2 += 1;
                    break;
                case 2:
                    for (r = N-1;a2[i][r]<1;r--) a2[i][r] = 2;
                    d2 += 1;
                    break;
                case 3:
                    for (u = 0;a2[u][j]<1;u++) a2[u][j] = 2;
                    d2 += 1;
                    break;
                case 4:
                    for (d = N-1;a2[d][j]<1;d--) a2[d][j] = 2;
                    d2 += 1;
                    break;
                case -1:
                    break;
                }
 
            }
        }
    }
 
    for (i = 0;i < N;i++) for (j = 0;j < N;j++)
        if (a2[i][j] == 2) result2 += 1;
}
int main()
{
    int realresult;
    cin >> T; t = 1;
    while (T--)
    {
        input(); cal1(); cal2();
        if (d1 > d2) realresult = result1;
        else if (d1 == d2) realresult = min(result1, result2);
        else realresult = result2;
        cout << "#" << t << " " << realresult << endl;
        t++;
    }
}


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

[SWEA 1949] 등산로 조정  (0) 2018.07.23
[SWEA 4014] 활주로 건설  (0) 2018.07.23
[SWEA 4013] 특이한 자석  (0) 2018.07.23
[SWEA 3752] 가능한 시험 점수  (0) 2018.07.23
[SWEA 1952] 수영장  (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
글 보관함