結果

問題 No.340 雪の足跡
ユーザー h_nosonh_noson
提出日時 2016-06-17 22:07:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 438 ms / 1,000 ms
コード長 2,828 bytes
コンパイル時間 660 ms
コンパイル使用メモリ 73,924 KB
実行使用メモリ 23,000 KB
最終ジャッジ日時 2023-07-30 21:36:12
合計ジャッジ時間 8,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,532 KB
testcase_01 AC 2 ms
5,652 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
5,408 KB
testcase_04 AC 2 ms
5,528 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
5,400 KB
testcase_07 AC 2 ms
5,428 KB
testcase_08 AC 2 ms
5,432 KB
testcase_09 AC 2 ms
5,460 KB
testcase_10 AC 2 ms
5,472 KB
testcase_11 AC 4 ms
7,624 KB
testcase_12 AC 3 ms
7,612 KB
testcase_13 AC 4 ms
7,748 KB
testcase_14 AC 20 ms
10,428 KB
testcase_15 AC 26 ms
12,748 KB
testcase_16 AC 19 ms
10,224 KB
testcase_17 AC 32 ms
12,704 KB
testcase_18 AC 26 ms
12,772 KB
testcase_19 AC 31 ms
12,704 KB
testcase_20 AC 297 ms
22,940 KB
testcase_21 AC 176 ms
5,420 KB
testcase_22 AC 35 ms
22,872 KB
testcase_23 AC 338 ms
23,000 KB
testcase_24 AC 291 ms
22,884 KB
testcase_25 AC 313 ms
22,872 KB
testcase_26 AC 50 ms
10,348 KB
testcase_27 AC 10 ms
5,624 KB
testcase_28 AC 43 ms
10,552 KB
testcase_29 AC 353 ms
22,052 KB
testcase_30 AC 237 ms
15,692 KB
testcase_31 AC 355 ms
21,784 KB
testcase_32 AC 415 ms
22,948 KB
testcase_33 AC 323 ms
22,876 KB
testcase_34 AC 438 ms
22,880 KB
testcase_35 AC 327 ms
22,952 KB
testcase_36 AC 319 ms
22,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP (i,0,(int)(n)-1)
#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP (i,(int)(n)-1,0)
#define INF (int)1e8
#define MOD (int)(1e9+7)

#define RIGHT 3
#define LEFT 1
#define UP 0
#define DOWN 2

typedef long long ll;

int pass[1000][1000][4];
int dist[1000][1000];

int main(void) {
    int i, j, w, h, n;
    cin >> w >> h >> n;
    rep (i,n) {
        int m, pre;
        cin >> m >> pre;
        rep (j,m) {
            int b;
            cin >> b;
            if (pre / w == b / w) {
                int y = pre / w;
                int from = pre % w;
                int to = b % w;
                if (from < to) {
                    pass[y][from][RIGHT]++;
                    pass[y][to][RIGHT]--;
                    pass[y][to][LEFT]++;
                    pass[y][from][LEFT]--;
                }
                else {
                    pass[y][from][LEFT]++;
                    pass[y][to][LEFT]--;
                    pass[y][to][RIGHT]++;
                    pass[y][from][RIGHT]--;
                }
            }
            else {
                int x = pre % w;
                int from = pre / w;
                int to = b / w;
                if (from < to) {
                    pass[from][x][UP]++;
                    pass[to][x][UP]--;
                    pass[to][x][DOWN]++;
                    pass[from][x][DOWN]--;
                }
                else {
                    pass[from][x][DOWN]++;
                    pass[to][x][DOWN]--;
                    pass[to][x][UP]++;
                    pass[from][x][UP]--;
                }
            }
            pre = b;
        }
    }
    rep (i,h) rep (j,w-1) pass[i][j+1][RIGHT] += pass[i][j][RIGHT];
    rep (i,h) rrep (j,w-1) pass[i][j][LEFT] += pass[i][j+1][LEFT];
    rep (j,w) rep (i,h-1) pass[i+1][j][UP] += pass[i][j][UP];
    rep (j,w) rrep (i,h-1) pass[i][j][DOWN] += pass[i+1][j][DOWN];

    queue<pair<int,int>> q;
    rep (i,h) rep (j,w) dist[i][j] = INF;
    dist[0][0] = 0;
    q.push(make_pair(0,0));
    while (!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        int dxy[4] = {1,0,-1,0};
        rep (i,4) if (pass[y][x][i]) {
            int ny = y + dxy[i];
            int nx = x + dxy[(i+1)%4];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if (dist[ny][nx] > dist[y][x] + 1) {
                dist[ny][nx] = dist[y][x] + 1;
                q.push(make_pair(ny,nx));
            }
        }
    }
    if (dist[h-1][w-1] == INF)
        cout << "Odekakedekinai.." << endl;
    else
        cout << dist[h-1][w-1] << endl;
    return 0;
}
0