結果

問題 No.340 雪の足跡
ユーザー Hachimori
提出日時 2016-03-06 09:52:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 2,382 bytes
コンパイル時間 731 ms
コンパイル使用メモリ 69,492 KB
実行使用メモリ 11,596 KB
最終ジャッジ日時 2024-09-24 14:40:32
合計ジャッジ時間 12,749 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 28 TLE * 4
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void read()’:
main.cpp:30:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |     scanf("%d%d%d", &col, &row, &nRoute);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:34:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   34 |         scanf("%d", &nPt);
      |         ~~~~~^~~~~~~~~~~~
main.cpp:40:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |             scanf("%d", &v);
      |             ~~~~~^~~~~~~~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cstdio>
#define r first
#define c second
using namespace std;
typedef pair<int, int> Point;
const int BUF = 1005;


int dr[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0, -1};

int row, col;
bool canPass[BUF][BUF][4];


int sign(int v) {
    if (v > 0) return 1;
    if (v < 0) return -1;
    return 0;
}

void read() {
    memset(canPass, 0, sizeof(canPass));
    
    int nRoute;
    scanf("%d%d%d", &col, &row, &nRoute);
    
    for (int i = 0; i < nRoute; ++i) {
        int nPt;
        scanf("%d", &nPt);
        nPt++;
        
        vector<Point> ptList;
        for (int j = 0; j < nPt; ++j) {
            int v;
            scanf("%d", &v);
            ptList.push_back(Point(v / col, v % col));
        }
        
        for (int j = 0; j + 1 < nPt; ++j) {
            Point &cur = ptList[j];
            Point &nex = ptList[j + 1];
            for (int k = 0; k < 4; ++k) {
                if (!(sign(nex.r - cur.r) == sign(dr[k]) &&
                      sign(nex.c - cur.c) == sign(dc[k])))
                    continue;

                int cr = cur.r;
                int cc = cur.c;
                while (!(cr == nex.r && cc == nex.c)) {
                    canPass[cr][cc][k] = true;
                    canPass[cr + dr[k]][cc + dc[k]][(k + 2) % 4] = true;
                    cr += dr[k];
                    cc += dc[k];
                }
                
                break;
            }
        }
    }
}


void work() {
    static int cost[BUF][BUF];
    queue<Point> Q;
    
    memset(cost, -1, sizeof(cost));
    cost[0][0] = 0;
    Q.push(Point(0, 0));
    
    while (!Q.empty()) {
        Point cur = Q.front();
        Q.pop();
        
        if (cur.r == row - 1 && cur.c == col - 1) {
            cout << cost[cur.r][cur.c] << endl;
            return;
        }
        
        for (int i = 0; i < 4; ++i) {
            int nr = cur.r + dr[i];
            int nc = cur.c + dc[i];
            
            if (!canPass[cur.r][cur.c][i]) continue;
            if (!(0 <= nr && nr < row && 0 <= nc && nc < col)) continue;
            if (cost[nr][nc] != -1) continue;

            cost[nr][nc] = cost[cur.r][cur.c] + 1;
            Q.push(Point(nr, nc));
        }
    }

    cout << "Odekakedekinai.." << endl;
}


int main() {
    read();
    work();
    return 0;
}
0