結果

問題 No.340 雪の足跡
ユーザー parukiparuki
提出日時 2016-06-17 15:57:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 260 ms / 1,000 ms
コード長 2,706 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 188,864 KB
実行使用メモリ 33,484 KB
最終ジャッジ日時 2024-10-11 05:16:16
合計ジャッジ時間 6,997 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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