結果

問題 No.402 最も海から遠い場所
ユーザー roarisroaris
提出日時 2019-12-12 21:15:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 540 ms / 3,000 ms
コード長 1,707 bytes
コンパイル時間 1,580 ms
コンパイル使用メモリ 172,156 KB
実行使用メモリ 123,384 KB
最終ジャッジ日時 2023-09-07 21:59:24
合計ジャッジ時間 4,876 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,472 KB
testcase_01 AC 2 ms
5,484 KB
testcase_02 AC 3 ms
5,720 KB
testcase_03 AC 2 ms
5,516 KB
testcase_04 AC 2 ms
5,452 KB
testcase_05 AC 2 ms
5,464 KB
testcase_06 AC 2 ms
5,440 KB
testcase_07 AC 2 ms
5,484 KB
testcase_08 AC 2 ms
5,660 KB
testcase_09 AC 2 ms
5,612 KB
testcase_10 AC 2 ms
5,500 KB
testcase_11 AC 2 ms
5,596 KB
testcase_12 AC 2 ms
5,720 KB
testcase_13 AC 5 ms
6,568 KB
testcase_14 AC 4 ms
8,268 KB
testcase_15 AC 15 ms
13,544 KB
testcase_16 AC 20 ms
15,940 KB
testcase_17 AC 213 ms
53,960 KB
testcase_18 AC 540 ms
53,900 KB
testcase_19 AC 400 ms
123,384 KB
testcase_20 AC 364 ms
50,420 KB
testcase_21 AC 351 ms
86,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int H, W;
char S[3100][3100];
int dist[3100][3100];

void bfs() {
    queue<P> q;
    
    for (int i=0; i<H+2; i++) {
        for (int j=0; j<W+2; j++) {
            if (S[i][j]=='.') {
                q.push(P(i, j));
                dist[i][j] = 0;
            }
            else {
                dist[i][j] = -1;
            }
        }
    }
    
    while (q.size()) {
        P p = q.front(); q.pop();
        int cx=p.first, cy=p.second;
        
        for (int i=-1; i<=1; i++) {
            for (int j=-1; j<=1; j++) {
                int nx=cx+i, ny=cy+j;
                
                if (!(0<=nx && nx<H+2 && 0<=ny && ny<W+2)) {
                    continue;
                }
                
                if (dist[nx][ny]==-1) {
                    dist[nx][ny] = dist[cx][cy]+1;
                    q.push(P(nx, ny));
                }
            }
        }
    }
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    cin >> H >> W;
    
    for (int i=0; i<H+2; i++) {
        if (i==0 || i==H+1) {
            for (int j=0; j<W+2; j++) {
                S[i][j] = '.';
            }
        }
        else {
            string Si; cin >> Si;
            
            for (int j=0; j<W+2; j++) {
                if (j==0 || j==W+1) {
                    S[i][j] = '.';
                }
                else {
                    S[i][j] = Si[j-1];
                }
            }
        }
    }
    
    bfs();
    int ans = 0;
    
    for (int i=0; i<H+2; i++) {
        for (int j=0; j<W+2; j++) {
            ans = max(ans, dist[i][j]);
        }
    }
    
    cout << ans << endl;
}
0