結果

問題 No.340 雪の足跡
ユーザー roarisroaris
提出日時 2019-12-15 11:29:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,184 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 171,920 KB
実行使用メモリ 33,272 KB
最終ジャッジ日時 2023-09-15 15:30:22
合計ジャッジ時間 4,713 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
33,272 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int H, W;
vector<int> adj_list[1000010];
int dist[1000010];

int bfs() {
    for (int i=0; i<H*W; i++) {
        dist[i] = -1;
    }
    
    queue<int> q;
    q.push(0);
    dist[0] = 0;
    
    while (q.size()) {
        int v = q.front(); q.pop();
        
        for (auto& nv : adj_list[v]) {
            if (dist[nv]==-1) {
                dist[nv] = dist[v]+1;
                q.push(nv);
            }
        }
    }
    
    return dist[H*W-1];
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    cin >> W >> H;
    int N; cin >> N;
    int imos1[H][W];
    int imos2[H][W];

    for (int i=0; i<H+1; i++) {
        for (int j=0; j<W+1; j++) {
            imos1[i][j] = 0;
            imos2[i][j] = 0;
        }
    }
    
    for (int i=0; i<N; i++) {
        int Mi; cin >> Mi;
        int B[Mi+1];
        
        for (int j=0; j<Mi+1; j++) cin >> B[j];
        
        for (int j=0; j<Mi; j++) {
            int s=min(B[j], B[j+1]), t=max(B[j], B[j+1]);
            
            if (s/W==t/W) {
                imos1[s/W][s%W] += 1;
                imos1[t/W][t%W] -= 1;
            }
            else if (s%W==t%W) {
                imos2[s/W][s%W] += 1;
                imos2[t/W][t%W] -= 1;
            }
        }
    }

    for (int i=0; i<H; i++) {
        for (int j=1; j<W; j++) {
            imos1[i][j] += imos1[i][j-1];
        }
    }
    
    for (int i=0; i<W; i++) {
        for (int j=1; j<H; j++) {
            imos2[j][i] += imos2[j-1][i];
        }
    }
    
    for (int i=0; i<H; i++) {
        for (int j=0; j<W-1; j++) {
            if (imos1[i][j]>0) {
                adj_list[i*W+j].push_back(i*W+j+1);
                adj_list[i*W+j+1].push_back(i*W+j);
            }
        }
    }
    
    for (int i=0; i<H-1; i++) {
        for (int j=0; j<W; j++) {
            if (imos2[i][j]>0) {
                adj_list[i*W+j].push_back((i+1)*W+j);
                adj_list[(i+1)*W+j].push_back(i*W+j);
            }
        }
    }
    
    int ans = bfs();
    
    if (ans==-1) {
        cout << "Odekakedekinai.." << endl;
    }
    else {
        cout << ans << endl;
    }
}
0