結果

問題 No.340 雪の足跡
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-19 15:50:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 251 ms / 1,000 ms
コード長 3,319 bytes
コンパイル時間 1,100 ms
コンパイル使用メモリ 70,272 KB
実行使用メモリ 11,240 KB
最終ジャッジ日時 2023-10-23 18:28:11
合計ジャッジ時間 6,517 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 10 ms
4,348 KB
testcase_15 AC 12 ms
4,348 KB
testcase_16 AC 9 ms
4,348 KB
testcase_17 AC 16 ms
4,348 KB
testcase_18 AC 13 ms
4,348 KB
testcase_19 AC 15 ms
4,348 KB
testcase_20 AC 120 ms
11,000 KB
testcase_21 AC 79 ms
4,348 KB
testcase_22 AC 17 ms
11,236 KB
testcase_23 AC 150 ms
11,240 KB
testcase_24 AC 109 ms
11,240 KB
testcase_25 AC 128 ms
11,000 KB
testcase_26 AC 21 ms
4,348 KB
testcase_27 AC 6 ms
4,348 KB
testcase_28 AC 19 ms
4,348 KB
testcase_29 AC 195 ms
7,900 KB
testcase_30 AC 135 ms
6,580 KB
testcase_31 AC 211 ms
10,472 KB
testcase_32 AC 248 ms
11,000 KB
testcase_33 AC 198 ms
11,000 KB
testcase_34 AC 251 ms
11,000 KB
testcase_35 AC 143 ms
11,000 KB
testcase_36 AC 143 ms
11,000 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