結果

問題 No.340 雪の足跡
ユーザー face4face4
提出日時 2019-12-15 19:31:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 1,000 ms
コード長 2,248 bytes
コンパイル時間 717 ms
コンパイル使用メモリ 76,300 KB
実行使用メモリ 15,388 KB
最終ジャッジ日時 2023-09-15 15:41:10
合計ジャッジ時間 6,751 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,508 KB
testcase_01 AC 4 ms
9,508 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
9,564 KB
testcase_04 AC 2 ms
7,520 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 3 ms
9,488 KB
testcase_07 AC 2 ms
5,420 KB
testcase_08 AC 3 ms
9,636 KB
testcase_09 AC 5 ms
9,320 KB
testcase_10 AC 4 ms
9,492 KB
testcase_11 AC 5 ms
9,504 KB
testcase_12 AC 5 ms
9,556 KB
testcase_13 AC 6 ms
11,620 KB
testcase_14 AC 21 ms
13,660 KB
testcase_15 AC 25 ms
13,592 KB
testcase_16 AC 20 ms
13,716 KB
testcase_17 AC 31 ms
13,724 KB
testcase_18 AC 25 ms
13,580 KB
testcase_19 AC 31 ms
13,656 KB
testcase_20 AC 281 ms
15,160 KB
testcase_21 AC 181 ms
9,316 KB
testcase_22 AC 12 ms
11,176 KB
testcase_23 AC 324 ms
15,104 KB
testcase_24 AC 268 ms
15,388 KB
testcase_25 AC 288 ms
15,096 KB
testcase_26 AC 49 ms
13,584 KB
testcase_27 AC 12 ms
9,504 KB
testcase_28 AC 43 ms
13,724 KB
testcase_29 AC 309 ms
15,168 KB
testcase_30 AC 207 ms
13,588 KB
testcase_31 AC 319 ms
14,812 KB
testcase_32 AC 368 ms
15,224 KB
testcase_33 AC 269 ms
15,172 KB
testcase_34 AC 366 ms
15,268 KB
testcase_35 AC 297 ms
15,116 KB
testcase_36 AC 304 ms
15,172 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] = {}, dp[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]--;
            }else if(j == nj){
                if(i > ni)  swap(i, ni);
                tate[i][j]++;
                tate[ni][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();
    }
    memset(dp, 0x3f, sizeof(dp));
    // for(int i = 0; i < h; i++){
    //     for(int j = 0; j < w; j++){
    //         cout << tate[i][j] << " \n"[j==w-1];
    //     }
    // }
    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-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){
            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-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){
            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