結果

問題 No.340 雪の足跡
ユーザー face4face4
提出日時 2019-12-15 19:12:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,174 bytes
コンパイル時間 712 ms
コンパイル使用メモリ 75,908 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2023-09-15 15:40:47
合計ジャッジ時間 6,863 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,692 KB
testcase_01 AC 2 ms
5,416 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
5,408 KB
testcase_04 AC 2 ms
5,480 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
5,468 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
5,424 KB
testcase_09 AC 2 ms
5,408 KB
testcase_10 AC 2 ms
5,500 KB
testcase_11 AC 3 ms
5,632 KB
testcase_12 AC 3 ms
5,628 KB
testcase_13 AC 3 ms
7,760 KB
testcase_14 AC 19 ms
8,704 KB
testcase_15 AC 23 ms
8,888 KB
testcase_16 AC 18 ms
8,360 KB
testcase_17 AC 30 ms
8,984 KB
testcase_18 AC 24 ms
9,044 KB
testcase_19 AC 30 ms
8,980 KB
testcase_20 AC 287 ms
15,360 KB
testcase_21 AC 176 ms
5,528 KB
testcase_22 AC 12 ms
11,500 KB
testcase_23 WA -
testcase_24 AC 270 ms
15,152 KB
testcase_25 AC 304 ms
15,156 KB
testcase_26 AC 47 ms
8,396 KB
testcase_27 AC 11 ms
5,420 KB
testcase_28 AC 42 ms
8,844 KB
testcase_29 AC 318 ms
13,432 KB
testcase_30 AC 222 ms
11,216 KB
testcase_31 AC 331 ms
14,764 KB
testcase_32 AC 384 ms
15,164 KB
testcase_33 AC 286 ms
15,216 KB
testcase_34 AC 372 ms
15,088 KB
testcase_35 AC 299 ms
15,152 KB
testcase_36 AC 302 ms
15,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int di[8] = {0,0,1,-1,1,1,-1,-1};
int dj[8] = {1,-1,0,0,1,-1,1,-1};
#define inRange(x,a,b) (a <= x && x < b)

int yoko[1001][1001] = {}, tate[1001][1001] = {};

void no(){
    cout << "Odekakedekinai.." << endl;
    exit(0);
}

int main(){
    int w, h, n;
    cin >> w >> h >> n;
    while(n--){
        int m;  cin >> m;
        int x;  cin >> x;
        while(m--){
            int y;  cin >> y;
            int i = x/w, j = x%w, ni = y/w, nj = y%w;
            if(i == ni){
                if(j > nj)  swap(j, nj);
                yoko[i][j]++;
                yoko[i][nj+1]--;
            }else if(j == nj){
                if(i > ni)  swap(i, ni);
                tate[i][j]++;
                tate[ni+1][j]--;
            }
            x = y;
        }
    }
    for(int i = 0; i < h; i++){
        for(int j = 1; j < w; j++){
            yoko[i][j] += yoko[i][j-1];
        }
    }
    for(int j = 0; j < w; j++){
        for(int i = 1; i < h; i++){
            tate[i][j] += tate[i-1][j];
        }
    }
    // :(
    if(h==1&&w==1){
        cout << 0 << endl;
        return 0;
    }
    if(tate[0][0]==0&&yoko[0][0]==0){
        no();
    }
    int dp[h][w];
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()){
        int x = q.front();  q.pop();
        int i = x/w, j = x%w;
        if(i != 0 && dp[i-1][j] > dp[i][j]+1 && tate[i][j]>0 && tate[i-1][j]>0){
            dp[i-1][j] = dp[i][j]+1;
            q.push((i-1)*w+j);
        }
        if(i != h-1 && dp[i+1][j] > dp[i][j]+1 && tate[i][j]>0 && tate[i+1][j]>0){
            dp[i+1][j] = dp[i][j]+1;
            q.push((i+1)*w+j);
        }
        if(j != 0 && dp[i][j-1] > dp[i][j]+1 && yoko[i][j]>0 && yoko[i][j-1]>0){
            dp[i][j-1] = dp[i][j]+1;
            q.push(i*w+(j-1));
        }
        if(j != w-1 && dp[i][j+1] > dp[i][j]+1 && yoko[i][j]>0 && yoko[i][j+1]>0){
            dp[i][j+1] = dp[i][j]+1;
            q.push(i*w+(j+1));
        }
    }
    if(dp[h-1][w-1]>1e9)    no();
    else                    cout << dp[h-1][w-1] << endl;
    return 0;
}
0