結果

問題 No.334 門松ゲーム
ユーザー hanorverhanorver
提出日時 2016-02-03 11:51:14
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,201 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 71,252 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 18:48:31
合計ジャッジ時間 1,554 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 WA -
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 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 6 ms
4,348 KB
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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){
            if(v[i] == v[j]){
                for(int k = 0; k < v.size(); ++k) kadomatsu[i][j][k] = false;
                continue;
            }
            for(int k = j + 1; k < v.size(); ++k){
                if((v[i] < v[j] && v[j] < v[k]) || v[i] == v[k] || v[j] == v[k]){
                    kadomatsu[i][j][k] = false;
                }else{
                    kadomatsu[i][j][k] = true;
                }
            }
        }
    }
}

void showvector(std::vector<int> v){
    for(int i = 0; i < v.size(); ++i){
        if(v[i] != 0){
            std::cout << v[i] << " ";
        }
    }
    std::cout << std::endl;
}

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 % 2 == 0){
            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, turn + 1));
                v[k] = c;
                if(turn % 2 == 0 && result.back() == true) return true;
            }
            v[j] = b;
        }
        v[i] = a;
    }
    if(turn % 2 == 1 && std::find(result.begin(), result.end(), false) == result.end()){
        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