답안 #12850

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
12850 2015-01-10T02:33:18 Z yongwhan 토마토 (KOI13_tomato) C++
16 / 16
136 ms 5716 KB
#include<algorithm>
#include<iostream>
#include<queue>
#include<utility>
using namespace std;

typedef pair<int,int> ii;

int main() {
 ios_base::sync_with_stdio(false); cin.tie(NULL);
 int m,n; cin>>m>>n;
 queue<ii> q;
 int vis[n][m];
 for (int i=0; i<n; i++) for (int j=0; j<m; j++) {
  cin>>vis[i][j];
  if(vis[i][j]==1) q.push(make_pair(i,j));
 }

 int ret=0;
 while(!q.empty()) {
  int sz=q.size();
  bool updated=false;
  while(sz--) {
   ii cur=q.front(); q.pop();
   for (int di=-1; di<=1; di++) for (int dj=-1; dj<=1; dj++) if(abs(di)+abs(dj)==1) {
    ii nxt=cur; nxt.first+=di; nxt.second+=dj;
    if(0<=nxt.first && nxt.first<n && 0<=nxt.second && nxt.second<m) {
     if(vis[nxt.first][nxt.second]==0) {
      vis[nxt.first][nxt.second]=1; q.push(nxt); updated=true;
     }
    }
   }
  }
  if(updated) ret++;
 }

 for (int i=0; i<n; i++) for (int j=0; j<m; j++) if(vis[i][j]==0) {
  cout << -1 << endl; return 0;
 }

 cout << ret << endl;
 return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1668 KB Output is correct
2 Correct 0 ms 1668 KB Output is correct
3 Correct 0 ms 1668 KB Output is correct
4 Correct 0 ms 1668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1668 KB Output is correct
2 Correct 0 ms 1668 KB Output is correct
3 Correct 0 ms 1668 KB Output is correct
4 Correct 0 ms 1668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1668 KB Output is correct
2 Correct 0 ms 1668 KB Output is correct
3 Correct 0 ms 1668 KB Output is correct
4 Correct 0 ms 1668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1808 KB Output is correct
2 Correct 4 ms 1820 KB Output is correct
3 Correct 0 ms 1800 KB Output is correct
4 Correct 0 ms 1668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1668 KB Output is correct
2 Correct 0 ms 1668 KB Output is correct
3 Correct 4 ms 1800 KB Output is correct
4 Correct 0 ms 1668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1668 KB Output is correct
2 Correct 0 ms 1692 KB Output is correct
3 Correct 0 ms 1668 KB Output is correct
4 Correct 4 ms 1704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 2936 KB Output is correct
2 Correct 16 ms 2356 KB Output is correct
3 Correct 12 ms 2076 KB Output is correct
4 Correct 8 ms 2064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 3344 KB Output is correct
2 Correct 44 ms 3120 KB Output is correct
3 Correct 8 ms 1888 KB Output is correct
4 Correct 4 ms 1844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 2740 KB Output is correct
2 Correct 100 ms 4904 KB Output is correct
3 Correct 12 ms 2068 KB Output is correct
4 Correct 56 ms 3664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 5584 KB Output is correct
2 Correct 136 ms 5716 KB Output is correct
3 Correct 124 ms 5452 KB Output is correct
4 Correct 88 ms 4668 KB Output is correct