Submission #544640

#TimeUsernameProblemLanguageResultExecution timeMemory
544640pokmui9909토마토 (KOI13_tomato)C++17
16 / 16
84 ms10568 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...