#include <iostream>
 
int R = 0;
int C = 0;
int T = 0;
int graph[51][51] = {};
int nextGraph[51][51] = {};
int DirX[4] = { 0, 1, 0, -1 };
int DirY[4] = { -1, 0, 1, 0 };
int upRow = 0;
int downRow = 0;
int total = 0;
 
void spreadDust()
{
    for (int i = 0; i < R; ++i)
    {
        for (int j = 0; j < C; ++j)
        {
            int count = 0;
            int value = graph[i][j] / 5;
 
            if (!graph[i][j] || graph[i][j] == -1)
                continue;
 
            for (int k = 0; k < 4; ++k)
            {
                int NextX = i + DirX[k];
                int NextY = j + DirY[k];
 
                if (NextX >= 0 && NextX < R &&
                    NextY >= 0 && NextY < C)
                {
                    if (graph[NextX][NextY] != -1)
                    {
                        nextGraph[NextX][NextY] += value;
                        ++count;
                    }
                }
            }
 
            nextGraph[i][j] -= (count * value);
        }
    }
 
    for (int i = 0; i < R; ++i)
    {
        for (int j = 0; j < C; ++j)
        {
            graph[i][j] += nextGraph[i][j];
            nextGraph[i][j] = 0;
        }
    }
}
 
void airCleaner()
{
    total -= graph[upRow - 1][0];
    total -= graph[downRow + 1][0];
 
    for (int i = upRow - 1; i > 0; --i)
    {
        graph[i][0] = graph[i - 1][0];
    }
 
    for (int i = 0; i < C - 1; ++i)
    {
        graph[0][i] = graph[0][i + 1];
    }
 
    for (int i = 1; i <= upRow; ++i)
    {
        graph[i - 1][C - 1] = graph[i][C - 1];
    }
 
    for (int i = C - 1; i > 1; --i)
    {
        graph[upRow][i] = graph[upRow][i - 1];
    }
 
    graph[upRow][1] = 0;
 
    for (int i = downRow + 1; i < R - 1; ++i)
    {
        graph[i][0] = graph[i + 1][0];
    }
 
    for (int i = 0; i < C - 1; ++i)
    {
        graph[R - 1][i] = graph[R - 1][i + 1];
    }
 
    for (int i = R - 1; i >= downRow; --i)
    {
        graph[i][C - 1] = graph[i - 1][C - 1];
    }
 
    for (int i = C - 1; i > 1; --i)
    {
        graph[downRow][i] = graph[downRow][i - 1];
    }
 
    graph[downRow][1] = 0;
}
 
int main()
{
    std::cin >> R >> C >> T;
 
    bool findUpAirCleaner = false;
 
    for (int i = 0; i < R; ++i)
    {
        for (int j = 0; j < C; ++j)
        {
            std::cin >> graph[i][j];
 
            if (graph[i][j] == -1)
            {
                if (!findUpAirCleaner)
                {
                    upRow = i;
                    findUpAirCleaner = true;
                }
 
                else
                {
                    downRow = i;
                }
            }
 
            else
            {
                total += graph[i][j];
            }
        }
    }
 
    while (T--)
    {
        spreadDust();
        airCleaner();
    }
 
    std::cout << total;
    return 0;
}
 

두개의 배열을 사용해서 구현하는 문제입니다.

말그대로 퍼졌다가 이동하는 걸 T만큼 연속적으로 이뤄지게 시키면 됩니다.

'오늘의 알고리즘' 카테고리의 다른 글

[C++] 백준 12852 1로 만들기 2  (0) 2022.11.11
[C++] 백준 1005 ACM Craft  (0) 2022.11.10
[C++] 백준 17070 파이프 옮기기 1  (0) 2022.11.07
[C++] 백준 16953 A → B  (0) 2022.11.06
[C++] 백준 15686 치킨 배달  (0) 2022.11.05

+ Recent posts