結果

問題 No.340 雪の足跡
ユーザー mamekinmamekin
提出日時 2016-01-29 22:49:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 538 ms / 1,000 ms
コード長 3,029 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 118,732 KB
実行使用メモリ 39,732 KB
最終ジャッジ日時 2023-10-21 17:11:59
合計ジャッジ時間 9,168 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 25 ms
6,400 KB
testcase_15 AC 30 ms
6,600 KB
testcase_16 AC 21 ms
5,004 KB
testcase_17 AC 38 ms
7,004 KB
testcase_18 AC 33 ms
7,048 KB
testcase_19 AC 38 ms
7,004 KB
testcase_20 AC 356 ms
39,700 KB
testcase_21 AC 190 ms
4,348 KB
testcase_22 AC 71 ms
39,700 KB
testcase_23 AC 538 ms
39,696 KB
testcase_24 AC 340 ms
39,696 KB
testcase_25 AC 372 ms
39,704 KB
testcase_26 AC 50 ms
4,348 KB
testcase_27 AC 11 ms
4,348 KB
testcase_28 AC 46 ms
5,400 KB
testcase_29 AC 386 ms
24,796 KB
testcase_30 AC 260 ms
19,684 KB
testcase_31 AC 435 ms
36,644 KB
testcase_32 AC 499 ms
39,732 KB
testcase_33 AC 422 ms
39,732 KB
testcase_34 AC 500 ms
39,732 KB
testcase_35 AC 451 ms
39,732 KB
testcase_36 AC 446 ms
39,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int minDist(const vector<string>& plane, char wall, int y1, int x1, int y2, int x2)
{
    int dy[] = {0, 0, -1, 1};
    int dx[] = {-1, 1, 0, 0};

    int h = plane.size();
    int w = plane[0].size();

    queue<pair<int, int> > q;
    vector<vector<bool> > check(h, vector<bool>(w, false));
    q.push(make_pair(y1, x1));
    check[y1][x1] = true;
    int ret = 0;
    while(!q.empty()){
        queue<pair<int, int> > q1;
        while(!q.empty()){
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
            if(y == y2 && x == x2)
                return ret;
            for(int i=0; i<4; ++i){
                int y0 = y + dy[i];
                int x0 = x + dx[i];
                if(0 <= y0 && y0 < h && 0 <= x0 && x0 < w && plane[y0][x0] != wall && !check[y0][x0]){
                    q1.push(make_pair(y0, x0));
                    check[y0][x0] = true;
                }
            }
        }
        ++ ret;
        q = q1;
    }
    return -1;
}

int main()
{
    int w, h, n;
    cin >> w >> h >> n;
    vector<string> s(2*h+1, string(2*w+1, '#'));
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            s[2*y+1][2*x+1] = '.';
        }
    }

    vector<vector<int> > a(2*h+1, vector<int>(2*w+1, 0));
    vector<vector<int> > b(2*h+1, vector<int>(2*w+1, 0));
    for(int i=0; i<n; ++i){
        int m;
        cin >> m;
        vector<int> y(m+1), x(m+1);
        for(int j=0; j<m+1; ++j){
            int b;
            cin >> b;
            y[j] = b / w;
            x[j] = b % w;
        }
        for(int j=1; j<m+1; ++j){
            int y1 = 2 * y[j-1] + 1;
            int x1 = 2 * x[j-1] + 1;
            int y2 = 2 * y[j] + 1;
            int x2 = 2 * x[j] + 1;
            if(y1 == y2){
                if(x2 < x1)
                    swap(x1, x2);
                ++ a[y1][x1];
                -- a[y1][x2];
            }
            else{
                if(y2 < y1)
                    swap(y1, y2);
                ++ b[y1][x1];
                -- b[y2][x1];
            }
        }
    }

    for(int y=0; y<2*h+1; ++y){
        int sum = 0;
        for(int x=0; x<2*w+1; ++x){
            sum += a[y][x];
            if(sum > 0)
                s[y][x] = '.';
        }
    }
    for(int x=0; x<2*w+1; ++x){
        int sum = 0;
        for(int y=0; y<2*h+1; ++y){
            sum += b[y][x];
            if(sum > 0)
                s[y][x] = '.';
        }
    }

    int ans = minDist(s, '#', 1, 1, 2 * h - 1, 2 * w - 1);
    if(ans == -1)
        cout << "Odekakedekinai.." << endl;
    else
        cout << (ans / 2) << endl;

    return 0;
}
0