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 | /* 2차원배열 하나만 사용해서 풀기 성공. */ #include<iostream> #include<queue> using namespace std; int arr[1001][1001]; int M, N; // M : 가로, N : 세로. int dx[4] = { 1, 0, -1, 0 }; int dy[4] = { 0, 1, 0, -1 }; int main(void) { queue<pair <int, int> > q; cin >> M >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { cin >> arr[i][j]; if (arr[i][j] == 1) { q.push(make_pair(i, j)); // 세로, 가로 } } } while (!q.empty()) { int x = q.front().first; // 세로 int y = q.front().second; // 가로 q.pop(); for (int i = 0; i < 4; i++) { int nx = x + dx[i]; // 세로 N int ny = y + dy[i]; // 가로 M //if (nx >= 1 && ny >= 1 && nx <= M && ny <= N) continue; // else이면 어쩔껀디? if (nx >= 1 && ny >= 1 && nx <= N && ny <= M) { if (arr[nx][ny] == 0) { arr[nx][ny] = arr[x][y] + 1; q.push(make_pair(nx, ny)); } } } } int max = 0; for (int i = 1; i < N; i++) { for (int j = 1; j < M; j++) { if (max < arr[i][j]) max = arr[i][j]; } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { if (arr[i][j] == 0) { cout << -1; return 0; } } } cout << max-1; return 0; } | cs |
'알고리즘 > BOJ' 카테고리의 다른 글
1987 알파벳 (실패) (0) | 2018.02.19 |
---|---|
6603 로또 (0) | 2018.02.14 |
1941 소문난 칠공주 (0) | 2018.02.14 |
2146 다리만들기 (0) | 2018.02.12 |
7576 토마토 (이차원배열 2개 사용) (0) | 2018.02.07 |