結果

問題 No.340 雪の足跡
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-19 15:50:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 204 ms / 1,000 ms
コード長 3,319 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 69,984 KB
実行使用メモリ 11,208 KB
最終ジャッジ日時 2024-09-22 12:16:31
合計ジャッジ時間 4,585 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 9 ms
6,940 KB
testcase_15 AC 11 ms
6,944 KB
testcase_16 AC 9 ms
6,940 KB
testcase_17 AC 14 ms
6,940 KB
testcase_18 AC 12 ms
6,940 KB
testcase_19 AC 15 ms
6,944 KB
testcase_20 AC 117 ms
11,016 KB
testcase_21 AC 79 ms
6,944 KB
testcase_22 AC 16 ms
11,052 KB
testcase_23 AC 152 ms
11,068 KB
testcase_24 AC 107 ms
11,044 KB
testcase_25 AC 126 ms
11,004 KB
testcase_26 AC 21 ms
6,940 KB
testcase_27 AC 5 ms
6,944 KB
testcase_28 AC 18 ms
6,940 KB
testcase_29 AC 146 ms
7,680 KB
testcase_30 AC 98 ms
6,944 KB
testcase_31 AC 169 ms
10,420 KB
testcase_32 AC 204 ms
11,016 KB
testcase_33 AC 160 ms
10,996 KB
testcase_34 AC 203 ms
11,208 KB
testcase_35 AC 140 ms
11,064 KB
testcase_36 AC 140 ms
11,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <ios>
#include <numeric>
#include <vector>
#include <stack>
#include <utility>
using namespace std;

void read_data(int W, int H, int N, vector<int> & map_hrzn, vector<int> & map_vrtc);

int solve(int W, int H, const vector<int> & map_hrzn, const vector<int> & map_vrtc);

int main()
{
    ios::sync_with_stdio(false);
    int W = 0;
    int H = 0;
    int N = 0;
    cin >> W >> H >> N;
    vector<int> map_hrzn(W * H, 0);
    vector<int> map_vrtc(W * H, 0);
    read_data(W, H, N, map_hrzn, map_vrtc);
    partial_sum(map_hrzn.begin(), map_hrzn.end(), map_hrzn.begin());
    partial_sum(map_vrtc.begin(), map_vrtc.end(), map_vrtc.begin());
    auto ans = solve(W, H, map_hrzn, map_vrtc);
    if (ans == -1){
        cout << "Odekakedekinai.." << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}

void read_data(int W, int H, int N, vector<int> & map_hrzn, vector<int> & map_vrtc){
    int M = 0;
    for (int i = 0; i != N; ++i){
        cin >> M;
        int next_b, prev_b, next_w, next_h, prev_w, prev_h;
        cin >> prev_b;
        prev_w = prev_b % W;
        prev_h = prev_b / W;
        for (int m = 0; m != M; ++m){
            cin >> next_b;
            next_w = next_b % W;
            next_h = next_b / W;
            if (prev_w == next_w){
                if (prev_h < next_h){
                    map_vrtc[prev_w * H + prev_h] += 1;
                    map_vrtc[next_w * H + next_h] -= 1;
                }else{
                    map_vrtc[prev_w * H + prev_h] -= 1;
                    map_vrtc[next_w * H + next_h] += 1;
                }
            }else{
                if (prev_b < next_b){
                    map_hrzn[prev_b] += 1;
                    map_hrzn[next_b] -= 1;
                }else{
                    map_hrzn[prev_b] -= 1;
                    map_hrzn[next_b] += 1;
                }
            }
            prev_w = next_w;
            prev_h = next_h;
            prev_b = next_b;
        }
    }    
}

int solve(int W, int H, const vector<int> & map_hrzn, const vector<int> & map_vrtc){
    stack<int> prev_que;
    stack<int> next_que;
    vector<bool> visited(H * W, false);
    const int goal = H * W - 1;
    prev_que.push(0);
    for (int steps = 0; steps != H * W; ++steps){
        while (not prev_que.empty()){
            auto b_hrzn = prev_que.top();
            if (b_hrzn == goal) return steps;
            prev_que.pop();
            int w = b_hrzn % W;
            int h = b_hrzn / W;
            int b_vrtc = w * H + h;
            if (w and map_hrzn[b_hrzn - 1] and not visited[b_hrzn - 1]){
                next_que.push(b_hrzn - 1);
                visited[b_hrzn - 1] = true;
            }
            if (w != W - 1 and map_hrzn[b_hrzn] and not visited[b_hrzn + 1]){
                next_que.push(b_hrzn + 1);
                visited[b_hrzn + 1] = true;
            }
            if (h and map_vrtc[b_vrtc - 1] and not visited[b_hrzn - W]){
                next_que.push(b_hrzn - W);
                visited[b_hrzn - W] = true;
            }
            if (h != H - 1 and map_vrtc[b_vrtc] and not visited[b_hrzn + W]){
                next_que.push(b_hrzn + W);
                visited[b_hrzn + W] = true;
            }            
        }
        swap(prev_que, next_que);
    }
    return -1;
}
0