結果

問題 No.334 門松ゲーム
ユーザー hanorverhanorver
提出日時 2016-02-03 14:22:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 3,187 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 70,556 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 18:49:11
合計ジャッジ時間 1,368 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

#define PLAYER 0
#define OPPONENT 1

bool kadomatsu[12][12][12];


void makeKadomatsu(std::vector<int> v){
    for(int i = 0;i < v.size() - 2; ++i){
        for(int j = i + 1; j < v.size() - 1; ++j){
            for(int k = j + 1; k < v.size(); ++k){
                if(v[i] == v[k] || v[j] == v[k] || v[i] == v[j]){
                    kadomatsu[i][j][k] = false;
                }else if((v[i] < v[j] && v[k] < v[j]) || (v[i] > v[j] && v[j] < v[k])){
                    kadomatsu[i][j][k] = true;
                }else{
                    kadomatsu[i][j][k] = false;
                }
            }
        }
    }
}

bool remaining_kadomatsu_array(std::vector<int> v){
    if(v.size() - std::count(v.begin(), v.end(), 0) < 3) return false;
    for(int i = 0; i < v.size() - 2; ++i){
        for(int j = i + 1; j < v.size() - 1; ++j){
            for(int k = j + 1; k < v.size(); ++k){
                if(kadomatsu[i][j][k] == true) return true;
            }
        }
    }
    return false;
}

bool dfs(std::vector<int> v, int turn){
    // if turn is even -> player's turn.
    if(remaining_kadomatsu_array(v) == false){
        if(turn == PLAYER){
            return false;
        }else{
            return true;
        }
    }
    std::vector<bool> result;
    for(int i = 0; i < v.size() - 2; ++i){
        if(v[i] == 0) continue; // 使用済みならパス
        int a = v[i];
        v[i] = 0;
        for(int j = i + 1; j < v.size() - 1; ++j){
            if(v[j] == 0) continue; // 使用済みならパス
            int b = v[j];
            v[j] = 0;
            for(int k = j + 1; k < v.size(); ++k){
                if(kadomatsu[i][j][k] == false) continue; // 門松列を作れない
                if(v[k] == 0) continue; // 使用済みならパス
                int c = v[k];
                v[k] = 0;
                result.push_back(dfs(v, 1 - turn));
                v[k] = c;

                // 自分のターンで、勝てる方法がある
                if(turn == PLAYER && result.back() == true) return true;
            }
            v[j] = b;
        }
        v[i] = a;
    }

    if(turn == OPPONENT && std::count(result.begin(), result.end(), true) == result.size()){
        // 相手のターンだが相手が何をしようが自分が勝つ場合
        return true;
    }else{
        return false;
    }
}

int main(){
    int n;
    std::cin >> n;

    std::vector<int> v(n);
    for(int i = 0; i < n; ++i){
        std::cin >> v[i];
    }
    makeKadomatsu(v);

    for(int i = 0; i < n - 2; ++i){
        int a = v[i];
        for(int j = i + 1; j < n - 1; ++j){
            int b = v[j];
            for(int k = j + 1; k < n; ++k){
                if(kadomatsu[i][j][k] == false) continue;
                int c = v[k];
                v[i] = v[j] = v[k] = 0;
                if(dfs(v, 1) == true){
                    std::cout << i << " " << j << " " << k << std::endl;
                    return 0;
                }
                v[k] = c;
            }
            v[j] = b;
        }
        v[i] = a;
    }
    std::cout << -1 << std::endl;
    return 0;
}
0