Submission #544640

# Submission time Handle Problem Language Result Execution time Memory
544640 2022-04-02T14:10:32 Z pokmui9909 토마토 (KOI13_tomato) C++17
16 / 16
84 ms 10568 KB
#include <bits/stdc++.h>
using namespace std;

int n, m;
int xdir[4] = {0, 0, 1, -1};
int ydir[4] = {1, -1, 0, 0};
int ar[1001][1001], visited[1001][1001];

struct point
{
    int x, y, d;
};

int bfs(int d)
{
    queue<point> q;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m ;j++)
        {
            if(ar[i][j] == 1)
            {
                visited[i][j] = 1;
                q.push({i, j, d});
            }
        }
    }
    int md = 0;
    while(!q.empty())
    {
        md = max(md, q.front().d);
        for(int i = 0; i < 4; i++)
        {
            int nx = q.front().x + xdir[i], ny = q.front().y + ydir[i], nd = q.front().d + 1;
            if(0 <= nx && nx < n && 0 <= ny && ny < m)
            {
                if(!visited[nx][ny] && ar[nx][ny] == 0)
                {
                    visited[nx][ny] = 1;
                    ar[nx][ny] = 1;
                    q.push({nx, ny, nd});
                }
            }
        }
        q.pop();
    }
    return md;
}

int main()
{
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(false);

    cin >> m >> n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> ar[i][j];
    int ans = bfs(0);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(ar[i][j] == 0)
            {
                cout << "-1";
                return 0;
            }
        }
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 456 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1876 KB Output is correct
2 Correct 4 ms 2004 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1108 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 6 ms 4052 KB Output is correct
4 Correct 4 ms 3416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1748 KB Output is correct
2 Correct 3 ms 2004 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 4 ms 1908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 5324 KB Output is correct
2 Correct 20 ms 8660 KB Output is correct
3 Correct 10 ms 3044 KB Output is correct
4 Correct 9 ms 3024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 7420 KB Output is correct
2 Correct 29 ms 6708 KB Output is correct
3 Correct 5 ms 1620 KB Output is correct
4 Correct 6 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 4256 KB Output is correct
2 Correct 63 ms 9584 KB Output is correct
3 Correct 9 ms 1572 KB Output is correct
4 Correct 40 ms 7912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10200 KB Output is correct
2 Correct 84 ms 10484 KB Output is correct
3 Correct 74 ms 10568 KB Output is correct
4 Correct 57 ms 8536 KB Output is correct