結果

問題 No.402 最も海から遠い場所
ユーザー tottoripapertottoripaper
提出日時 2016-10-23 19:20:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 607 ms / 3,000 ms
コード長 1,598 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 168,800 KB
実行使用メモリ 122,364 KB
最終ジャッジ日時 2024-05-03 04:55:07
合計ジャッジ時間 5,954 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
38,912 KB
testcase_01 AC 27 ms
38,784 KB
testcase_02 AC 27 ms
39,040 KB
testcase_03 AC 28 ms
38,912 KB
testcase_04 AC 28 ms
38,784 KB
testcase_05 AC 28 ms
38,912 KB
testcase_06 AC 27 ms
39,040 KB
testcase_07 AC 26 ms
38,912 KB
testcase_08 AC 27 ms
38,912 KB
testcase_09 AC 27 ms
38,912 KB
testcase_10 AC 28 ms
38,912 KB
testcase_11 AC 27 ms
38,784 KB
testcase_12 AC 27 ms
38,912 KB
testcase_13 AC 30 ms
39,040 KB
testcase_14 AC 28 ms
39,040 KB
testcase_15 AC 39 ms
40,192 KB
testcase_16 AC 43 ms
40,448 KB
testcase_17 AC 237 ms
61,512 KB
testcase_18 AC 607 ms
52,440 KB
testcase_19 AC 366 ms
122,364 KB
testcase_20 AC 341 ms
48,624 KB
testcase_21 AC 338 ms
86,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

string ma[3000];
int dist[3002][3002];
stack<P> st, st2;

int main(){
    //*
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    /*/
      /*/

    int H, W;
    std::cin >> H >> W;

    memset(dist, -1, sizeof(dist));
    
    for(int i=0;i<=H+1;++i){
        dist[i][0] = 0;
        dist[i][W+1] = 0;
        st.emplace(i, 0);
        st.emplace(i, W+1);
    }

    for(int i=0;i<=W+1;++i){
        dist[0][i] = 0;
        dist[H+1][i] = 0;
        st.emplace(0, i);
        st.emplace(H+1, i);
    }
    
    for(int i=0;i<H;++i){
        std::cin >> ma[i];

        for(int j=0;j<W;++j){
            if(ma[i][j] == '.'){
                dist[i+1][j+1] = 0;
                st.emplace(i+1, j+1);
            }
        }
    }

    int res;
    while(!st.empty()){
        while(!st.empty()){
            int y, x;
            tie(y, x) = st.top(); st.pop();

            for(int i=0;i<8;++i){
                int ny = y + dy[i], nx = x + dx[i];
                if(1 <= nx && nx <= W &&
                   1 <= ny && ny <= H &&
                   dist[ny][nx] == -1){
                    dist[ny][nx] = dist[y][x] + 1;
                    res = dist[ny][nx];
                    st2.emplace(ny, nx);
                }
            }
        }

        swap(st, st2);
    }

    printf("%d\n", res);
}
0