結果

問題 No.340 雪の足跡
ユーザー parukiparuki
提出日時 2016-06-17 15:57:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 1,000 ms
コード長 2,706 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 183,940 KB
実行使用メモリ 33,588 KB
最終ジャッジ日時 2024-04-19 12:55:30
合計ジャッジ時間 5,882 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 14 ms
7,552 KB
testcase_15 AC 17 ms
8,064 KB
testcase_16 AC 13 ms
6,272 KB
testcase_17 AC 22 ms
8,704 KB
testcase_18 AC 18 ms
8,320 KB
testcase_19 AC 22 ms
8,540 KB
testcase_20 AC 159 ms
25,692 KB
testcase_21 AC 127 ms
19,108 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 155 ms
27,328 KB
testcase_24 AC 153 ms
16,776 KB
testcase_25 AC 142 ms
27,136 KB
testcase_26 AC 39 ms
7,808 KB
testcase_27 AC 10 ms
5,376 KB
testcase_28 AC 31 ms
7,360 KB
testcase_29 AC 196 ms
28,048 KB
testcase_30 AC 135 ms
19,584 KB
testcase_31 AC 196 ms
28,556 KB
testcase_32 AC 228 ms
33,588 KB
testcase_33 AC 185 ms
28,928 KB
testcase_34 AC 231 ms
32,896 KB
testcase_35 AC 179 ms
31,232 KB
testcase_36 AC 171 ms
27,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

typedef vector<pair<int, int>> Intervals;
Intervals mergeOpenedIntervals(Intervals a){
    Intervals res;
    sort(all(a));
    each(b, a){
        if(res.size() == 0 || res.back().second < b.first)
            res.push_back(b);
        else
            res.back().second = max(res.back().second, b.second);
    }
    return res;
}

const int DY[] = {-1,0,1,0};
const int DX[] = {0,1,0,-1};

const int MAX = 1000;
int W, H, N, ro[MAX][MAX][4], vis[MAX][MAX];
Intervals ir[MAX], ic[MAX];

void init(){
    cin >> W >> H >> N;
    rep(i, N){
        int M; cin >> M;
        vi B(M + 1);
        rep(j, M + 1)scanf("%d", &B[j]);
        rep(j, M){
            int ay = B[j] / W;
            int ax = B[j] % W;
            int by = B[j+1] / W;
            int bx = B[j+1] % W;
            if(ay > by)swap(ay, by);
            if(ax > bx)swap(ax, bx);
            if(ay == by)
                ir[by].emplace_back(ax, bx);
            if(ax == bx)
                ic[bx].emplace_back(ay, by);
        }
    }
    rep(y, H){
        ir[y] = mergeOpenedIntervals(ir[y]);
        each(in, ir[y]){
            int a, b;
            tie(a, b) = in;
            FOR(ax, a, b){
                int bx = ax + 1;
                ro[y][ax][1] = 1;
                ro[y][bx][3] = 1;
            }
        }
    }
    rep(x, W){
        ic[x] = mergeOpenedIntervals(ic[x]);
        each(in, ic[x]){
            int a, b;
            tie(a, b) = in;
            FOR(ay, a, b){
                int by = ay + 1;
                ro[ay][x][2] = 1;
                ro[by][x][0] = 1;
            }
        }
    }
}

void solve(){
    string NG = "Odekakedekinai..";
    queue<tuple<int, int, int>> Q;
    Q.push(make_tuple(0, 0, 0));
    while(sz(Q)){
        int y, x, d;
        tie(y, x, d) = Q.front();
        Q.pop();
        if(vis[y][x]++)continue;
        if(y == H - 1 && x == W - 1){
            cout << d << endl;
            return;
        }
        rep(i, 4)if(ro[y][x][i]){
            int ny = y + DY[i];
            int nx = x + DX[i];
            if(vis[ny][nx])continue;
            Q.push(make_tuple(ny, nx, d + 1));
        }
    }
    cout << NG << endl;
}

int main(){
    init();
    solve();
}
0