ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Softeer [level 3] : 동계 테스트 시점 예측 (C 언어)
    softeer 문제 2023. 4. 7. 15:44

    https://softeer.ai/practice/info.do?idx=1&eid=411&sw_prbl_sbms_sn=172894 

     

    Softeer

    연습문제를 담을 Set을 선택해주세요. 취소 확인

    softeer.ai

    Code:

    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    #include <stdio.h>
     
    int n, m;
    int arr[101][101= { 0, };
    int check[101][101= { 0, };
    int flag = 1;
    int dx[] = { -1100 };
    int dy[] = { 00- 11 };
     
    void copy()
    {
        flag = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                arr[i][j] = check[i][j];
                if (check[i][j] == 1)
                {
                    flag = 1;
                }
            }
        }
    }
     
    void look(int a, int b)
    {
        int count = 0;
        if (arr[a][b - 1== 2)
        {
            count ++ ;
        }
        if (arr[a + 1][b] == 2)
        {
            count++;
        }
        if (arr[a - 1][b] == 2)
        {
            count++;
        }
        if (arr[a][b + 1]==2)
        {
            count++;
        }
        if (count >= 2)
        {
            check[a][b] = 0;
        }
    }
     
    void dfs(int a, int b)
    {
        for (int k = 0; k < 4; k++)
        {
            int y = a + dy[k];
            int x = b + dx[k];
            if (y >= 0 && y < n && x >= 0 && x < m && arr[y][x] == 0)
            {
                arr[y][x] = 2;
                dfs(y, x);
            }
        }
    }
     
    int main()
    {
        int startcheck = 0;
        scanf("%d %d"&n, &m);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                scanf("%d"&arr[i][j]);
                check[i][j] = arr[i][j];
                if (arr[i][j] == 1)
                {
                    startcheck = 1;
                }
            }
        }
        int cnt = 0;
        int flag2 = 0;
        int flag3 = 0;
        if (startcheck == 0)
        {
            cnt = 0;
        }
        else
        {
            while (flag == 1)
            {
                cnt++;
                flag2 = 0;
                if (flag2 == 0)
                {
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < m; j++)
                        {
                            if (arr[i][j] == 0)
                            {
                                dfs(i, j);
                                flag2 = 1;
                                flag3 = 1;
                                break;
                            }
                        }
                        if (flag3 == 1)
                        {
                            break;
                        }
                    }
                }
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (arr[i][j] == 1)
                        {
                            look(i, j);
                        }
                    }
                }
                copy();
            }
        }
        printf("%d", cnt);
    }
     
    /*
    7 9
    0 0 0 0 0 0 0 0 0
    0 1 1 0 0 0 1 1 0
    0 1 0 1 1 1 0 1 0
    0 1 0 0 1 0 0 1 0
    0 1 0 1 1 1 0 1 0
    0 1 1 0 0 0 1 1 0
    0 0 0 0 0 0 0 0 0
    */
    cs

     

    문제풀이:

    1. 외부공기를 2 내부공기를 0 얼음을 1로 설정하였다.

    2. arr와 check 2개의 배열을 만들어 arr로 조건을 판단하고 check값을 바꾼 뒤 2중 for문을 한바퀴 돌면 copy()에 들어가 check값을 arr로 넣어준다.

    밑에 주석은 문제에 나와있는 두번 째 예제이다. 결과값은 3이 나와야 한다.

Designed by Tistory.