結果

問題 No.340 雪の足跡
ユーザー Tsuneo YoshiokaTsuneo Yoshioka
提出日時 2016-01-30 00:26:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 291 ms / 1,000 ms
コード長 4,232 bytes
コンパイル時間 1,263 ms
コンパイル使用メモリ 104,228 KB
実行使用メモリ 19,600 KB
最終ジャッジ日時 2023-10-21 17:38:56
合計ジャッジ時間 5,709 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 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 2 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 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 16 ms
4,600 KB
testcase_15 AC 20 ms
4,804 KB
testcase_16 AC 15 ms
4,492 KB
testcase_17 AC 26 ms
5,204 KB
testcase_18 AC 21 ms
4,884 KB
testcase_19 AC 25 ms
5,216 KB
testcase_20 AC 149 ms
15,920 KB
testcase_21 AC 111 ms
11,548 KB
testcase_22 AC 8 ms
7,528 KB
testcase_23 AC 193 ms
15,828 KB
testcase_24 AC 142 ms
19,532 KB
testcase_25 AC 137 ms
15,448 KB
testcase_26 AC 38 ms
5,856 KB
testcase_27 AC 9 ms
4,348 KB
testcase_28 AC 32 ms
5,692 KB
testcase_29 AC 236 ms
17,424 KB
testcase_30 AC 165 ms
13,352 KB
testcase_31 AC 254 ms
17,912 KB
testcase_32 AC 291 ms
19,600 KB
testcase_33 AC 236 ms
17,616 KB
testcase_34 AC 289 ms
19,540 KB
testcase_35 AC 199 ms
15,996 KB
testcase_36 AC 198 ms
15,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <fstream>
#include <iostream>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <numeric>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stdexcept>

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cassert>
#include <unistd.h>
#include <string.h>

#include <stack>
#include <list>

using namespace std;

int main(int argc, const char * argv[])
{
    // sleep(1000);
    // insert code here...
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(15);

    int W, H, N;
    cin >> W >> H >> N;
    vector< vector<bool> > maw(H, vector<bool>(W, false));
    vector< vector<bool> > mah(H, vector<bool>(W, false));
    vector< vector< pair<int, int> > >hlines(W);
    vector< vector< pair<int, int> > >wlines(H);
    
    for(int n=0;n<N;n++){
        int M;
        cin >> M; M++;
        int prev_h, prev_w;
        
        for(int m=0;m<M;m++){
            int B;
            cin >> B;
            int w, h;
            w = B%W;
            h = B/W;
             //   cout << "m=" << m << ",B=" << B << " w=" << w << ",h=" << h << endl;
            if(m>=1){
                if(prev_h == h){
                    int w1 = prev_w, w2 = w;
                    if(w2<w1){
                        swap(w1, w2);
                    }
                    //cout << "add wlines" << w1 << ", " << w2 << endl;
                    wlines[h].push_back(make_pair(w1, w2));
                }else if(prev_w == w){
                    int h1 = prev_h, h2 = h;
                    if(h2<h1){
                        swap(h1, h2);
                    }
                    hlines[w].push_back(make_pair(h1, h2));
                }else{
                    printf("Wrong......\n");
                    exit(1);
                }
            }
            prev_w = w;
            prev_h = h;
        }
    }

    for(int h=0;h<H;h++){
        sort(wlines[h].begin(), wlines[h].end());
        int endpos = -1;
        int cur = 0;
        bool prev=false;
        for(int w=0;w<W;w++){
            //cout << "cur=" << cur << ", h=" << h << ", w=" << w << endl;
            while(cur < wlines[h].size() && wlines[h][cur].first==w){
                //cout << "first=" << wlines[w][cur].first << ", second=" << wlines[w][cur].second << endl;
                endpos = max(endpos, wlines[h][cur].second);
                cur++;
            }
            if(w<=endpos){
                if(prev){
                    maw[h][w-1] = true;
                }
            }
            prev = (w<endpos);
        }
    }
    for(int w=0;w<W;w++){
        sort(hlines[w].begin(), hlines[w].end());
        int endpos = -1;
        int cur = 0;
        bool prev = false;
        for(int h=0;h<H;h++){
            while(cur < hlines[w].size() && hlines[w][cur].first==h){
                endpos = max(endpos, hlines[w][cur].second);
                cur++;
            }
            if(h<=endpos){
                if(prev){
                    mah[h-1][w] = true;
                }
            }
            prev = (h<endpos);
            
        }
    }
    queue< pair<int, int> > q;
    vector<int> used(W*H, false);
    
    q.push(make_pair(0,0));
    while(!q.empty()){
        auto pa = q.front();
        q.pop();
        int c = pa.first;
        int b = pa.second;
        if(used[b]){
            continue;
        }
        used[b] = true;
        //cout << "c=" << c << ", b=" << b << ", w=" << b%W << ", h=" << b/W << endl;
        if(b==W*H-1){
            cout << c << endl;
            exit(0);
        }
        
        int dx[]={0,1,0,-1};
        int dy[]={1,0,-1,0};
        for(int d=0;d<4;d++){
            int x = (b%W)+dx[d];
            int y = (b/W)+dy[d];
            if(x<0 || x>=W || y<0 || y>=H){
                continue;
            }
            int newb = y*W+x;
            if(
                (d==0 && mah[y-1][x]) ||
                (d==2 && mah[y][x]) ||
                (d==1 && maw[y][x-1]) ||
                (d==3 && maw[y][x])
                ){
                q.push(make_pair(c+1, newb));
            }
        }
    }
    cout << "Odekakedekinai.." << endl;
    return 0;
}


0