結果

問題 No.334 門松ゲーム
ユーザー sekiya9311sekiya9311
提出日時 2017-10-11 23:54:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 5,026 bytes
コンパイル時間 1,081 ms
コンパイル使用メモリ 110,332 KB
実行使用メモリ 33,384 KB
最終ジャッジ日時 2024-04-28 16:23:27
合計ジャッジ時間 2,820 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
25,552 KB
testcase_01 AC 32 ms
25,136 KB
testcase_02 AC 67 ms
29,536 KB
testcase_03 AC 32 ms
25,424 KB
testcase_04 AC 31 ms
25,424 KB
testcase_05 AC 33 ms
27,332 KB
testcase_06 AC 38 ms
25,556 KB
testcase_07 AC 45 ms
29,540 KB
testcase_08 AC 44 ms
31,596 KB
testcase_09 AC 40 ms
33,384 KB
testcase_10 AC 63 ms
27,616 KB
testcase_11 AC 47 ms
31,460 KB
testcase_12 AC 166 ms
29,300 KB
testcase_13 AC 51 ms
31,224 KB
testcase_14 AC 69 ms
31,448 KB
testcase_15 AC 181 ms
31,468 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgrammingContest
{
    class MainClass
    {
        Scanner sc;
        static void Main(string[] args)
        {
            new MainClass().Solve();
        }

        void Solve()
        {
#if DEBUG
            string backPath = "..";
            char dirSep = System.IO.Path.DirectorySeparatorChar;
            string inFilePath = backPath + dirSep + backPath + dirSep + "in.txt";
            sc = new Scanner(new System.IO.StreamReader(inFilePath));
#else
            sc = new Scanner();
#endif

            N = sc.NextInt;
            K = new int[N].Select(e => sc.NextInt).ToArray();
            var ans = dfs(0, true);
            if (ans)
            {
                Write(ansVals.Item1 + " " + ansVals.Item2 + " " + ansVals.Item3);
            }
            else
            {
                Write(-1);
            }
        }
        int N;
        int[] K;
        Tuple<int, int, int> ansVals = Tuple.Create(22, 22, 22);
        bool dfs(int now, bool isFirst)
        {
            bool isWin = false;
            for (int i = 0; i < N; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    for (int k = j + 1; k < N; k++)
                    {
                        if ((now & ((1 << i) | (1 << j) | (1 << k))) == 0)
                        {
                            int[] ar = new int[] { K[i], K[j], K[k] };
                            Array.Sort(ar);
                            if ((ar[1] == K[i] || ar[1] == K[k]) && ar[0] != ar[1] && ar[1] != ar[2])
                            {
                                bool buf = dfs(now | ((1 << i) | (1 << j) | (1 << k)), !isFirst);
                                if (!buf)
                                {
                                    var bufVals = Tuple.Create(i, j, k);
                                    if (now == 0 && comp(bufVals, ansVals) < 0)
                                    {
                                        ansVals = bufVals;
                                    }
                                    isWin = true;
                                }
                            }
                        }
                    }
                }
            }
            return isWin;
        }
        int comp(Tuple<int, int, int> a, Tuple<int, int, int> b)
        {
            if (a.Item1 < b.Item1)
            {
                return -1;
            }
            else if (a.Item1 == b.Item1)
            {
                if (a.Item2 < b.Item2)
                {
                    return -1;
                }
                else if (a.Item2 == b.Item2)
                {
                    if (a.Item3 < b.Item3)
                    {
                        return -1;
                    }
                    else if (a.Item3 == b.Item3)
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                return 1;
            }
        }

        void Write(object val)
        {
            Console.WriteLine(val.ToString());
        }
        void Write(string format, params object[] vals)
        {
            Console.WriteLine(format, vals);
        }
    }
    
    class Scanner
    {
        Queue<String> buffer;
        char[] sep;
        System.IO.TextReader reader;
        public Scanner(System.IO.TextReader reader = null)
        {
            this.buffer = new Queue<string>();
            this.sep = new char[] { ' ' };
            this.reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.buffer.Count == 0 && this.reader.Peek() >= 0)
            {
                string[] sreArray = this.reader.ReadLine().Split(this.sep);
                foreach (String elStr in sreArray)
                {
                    this.buffer.Enqueue(elStr);
                }
            }
        }

        public String Next
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Dequeue();
            }
        }

        public int NextInt
        {
            get
            {
                return int.Parse(this.Next);
            }
        }
        public double NextDouble
        {
            get
            {
                return double.Parse(this.Next);
            }
        }

        public long NextLong
        {
            get
            {
                return long.Parse(this.Next);
            }
        }
        public bool IsEmpty
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Count == 0;
            }
        }
    }
}
0