結果

問題 No.340 雪の足跡
ユーザー face4face4
提出日時 2019-12-15 19:07:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,093 bytes
コンパイル時間 697 ms
コンパイル使用メモリ 76,220 KB
実行使用メモリ 15,412 KB
最終ジャッジ日時 2023-09-15 15:38:06
合計ジャッジ時間 6,873 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,416 KB
testcase_01 AC 2 ms
5,476 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
5,436 KB
testcase_04 AC 2 ms
5,612 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,480 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 ms
5,520 KB
testcase_10 WA -
testcase_11 AC 4 ms
5,680 KB
testcase_12 AC 2 ms
5,568 KB
testcase_13 AC 4 ms
7,764 KB
testcase_14 AC 19 ms
8,792 KB
testcase_15 AC 23 ms
8,908 KB
testcase_16 AC 19 ms
8,424 KB
testcase_17 AC 30 ms
9,116 KB
testcase_18 AC 24 ms
9,092 KB
testcase_19 AC 30 ms
9,296 KB
testcase_20 AC 285 ms
15,292 KB
testcase_21 AC 177 ms
5,488 KB
testcase_22 AC 13 ms
11,316 KB
testcase_23 WA -
testcase_24 AC 267 ms
15,136 KB
testcase_25 WA -
testcase_26 AC 48 ms
8,524 KB
testcase_27 AC 10 ms
5,632 KB
testcase_28 AC 41 ms
8,908 KB
testcase_29 AC 315 ms
13,248 KB
testcase_30 AC 212 ms
11,232 KB
testcase_31 AC 327 ms
14,460 KB
testcase_32 AC 397 ms
15,104 KB
testcase_33 AC 294 ms
15,100 KB
testcase_34 AC 373 ms
15,100 KB
testcase_35 AC 308 ms
15,292 KB
testcase_36 AC 304 ms
15,092 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(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]==1<<30) no();
    else                    cout << dp[h-1][w-1] << endl;
    return 0;
}
0