結果

問題 No.124 門松列(3)
ユーザー ctyl_0ctyl_0
提出日時 2015-09-17 20:12:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 3,973 bytes
コンパイル時間 874 ms
コンパイル使用メモリ 91,052 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 12:23:47
合計ジャッジ時間 2,059 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

void inputArray(int * p, int a){
    rep(i, a){
        cin >> p[i];
    }
};

void inputVector(vector<int> * p, int a){
    rep(i, a){
        int input;
        cin >> input;
        p -> push_back(input);
    }
}

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

bool isKado(int a, int b, int c){
    
    if ((c < a && a < b) || (b < a && a < c) || (a < c && c < b) || (b < c && c < a)) {
        return true;
    }
    else{
        return false;
    }
}

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
//


int main(int argc, const char * argv[]) {
    
    // source code
    int W = inputValue(); // 2以上
    int H = inputValue(); // 2以上
    vector<vector<int>> M(W, vector<int>(H));
    vector<vector<vector<int>>> C(W, vector<vector<int>>(H, vector<int>(4))); //最短
    bool V[200][200][4];
    
    rep(j, H){
        rep(i, W){
            M[i][j] = inputValue();
            // V[i][j] = false;
            
            rep(k, 4){
                C[i][j][k] = 0;
                V[i][j][k] = false;
            }
        }
    }
    
    queue<pair<pair<int, int>, pair<int, int>>> q; // (今の座標, 一つ前にいた座標)
    rep(i, 4){
        V[0][0][i] = true;
    }
    if (H >= 3) {
        if (isKado(M[0][0], M[0][1], M[0][2])) {
            q.push(make_pair(make_pair(0, 2), make_pair(0, 1)));
            C[0][1][2] = 1;
            C[0][2][2] = 2;
            V[0][1][2] = true;
            V[0][2][2] = true;
        }
    }
    if (isKado(M[0][0], M[0][1], M[1][1])) {
        q.push(make_pair(make_pair(1, 1),make_pair(0, 1)));
        C[0][1][2] = 1;
        C[1][1][0] = 2;
        V[0][1][2] = true;
        V[1][1][0] = true;
    }
    if (isKado(M[0][0], M[1][0], M[1][1])) {
        q.push(make_pair(make_pair(1, 1), make_pair(1, 0)));
        C[1][0][0] = 1;
        C[1][1][2] = 2;
        V[1][0][0] = true;
        V[1][1][2] = true;
    }
    if (W >= 3) {
        if (isKado(M[0][0], M[1][0], M[2][0])) {
            q.push(make_pair(make_pair(2, 0), make_pair(1, 0)));
            C[1][0][0] = 1;
            C[2][0][0] = 2;
            V[1][0][0] = true;
            V[2][0][0] = true;
        }
    }
    
    
    while (!q.empty()) {
        int x = q.front().first.first;
        int y = q.front().first.second;
        int prevM = M[q.front().second.first][q.front().second.second];
        int pDir = -1;
        rep(i, 4){
            if (x - q.front().second.first == dx[i] && y - q.front().second.second == dy[i]) {
                pDir = i;
            }
        }
        q.pop();
        // V[x][y] = true;
        
        
        rep(i, 4){
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            
            if (nx < 0 || nx >= W || ny < 0 || ny >= H || V[nx][ny][i]) {
                continue;
            }
            
            if (isKado(prevM, M[x][y], M[nx][ny])) {
                
                
                C[nx][ny][i] = C[x][y][pDir] + 1;
                V[nx][ny][i] = true;
                if (nx == W - 1 && ny == H - 1) {
                    break;
                }
                q.push(make_pair(make_pair(nx, ny), make_pair(x, y)));
            }
            
        }
        
    }
    
    int retC = 0;
    rep(i, 4){
        if (C[W - 1][H - 1][i] != 0) {
            retC = (retC == 0) ? C[W - 1][H - 1][i] : min(retC, C[W - 1][H - 1][i]);
        }
    }
    
    output((retC != 0) ? retC : -1, 0);
    
    
    
    return 0;
}
0