結果

問題 No.483 マッチ並べ
ユーザー 14番14番
提出日時 2017-02-12 03:17:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 4,050 bytes
コンパイル時間 4,350 ms
コンパイル使用メモリ 109,496 KB
実行使用メモリ 24,264 KB
最終ジャッジ日時 2023-08-28 14:49:53
合計ジャッジ時間 9,063 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
23,920 KB
testcase_01 AC 87 ms
22,264 KB
testcase_02 AC 75 ms
23,896 KB
testcase_03 AC 82 ms
21,980 KB
testcase_04 AC 80 ms
21,984 KB
testcase_05 AC 81 ms
21,988 KB
testcase_06 AC 84 ms
24,264 KB
testcase_07 AC 85 ms
20,184 KB
testcase_08 AC 82 ms
24,020 KB
testcase_09 AC 82 ms
21,992 KB
testcase_10 AC 84 ms
20,040 KB
testcase_11 AC 84 ms
20,072 KB
testcase_12 AC 84 ms
22,228 KB
testcase_13 AC 81 ms
21,920 KB
testcase_14 AC 82 ms
21,924 KB
testcase_15 AC 85 ms
24,056 KB
testcase_16 AC 86 ms
22,180 KB
testcase_17 AC 86 ms
22,032 KB
testcase_18 AC 77 ms
23,832 KB
testcase_19 AC 83 ms
22,160 KB
testcase_20 AC 83 ms
22,052 KB
testcase_21 AC 83 ms
24,216 KB
testcase_22 AC 84 ms
24,160 KB
testcase_23 AC 83 ms
22,176 KB
testcase_24 AC 85 ms
22,160 KB
testcase_25 AC 83 ms
22,052 KB
testcase_26 AC 84 ms
24,144 KB
testcase_27 AC 86 ms
22,192 KB
testcase_28 AC 86 ms
22,152 KB
testcase_29 AC 85 ms
22,152 KB
testcase_30 AC 85 ms
22,056 KB
testcase_31 AC 86 ms
22,300 KB
testcase_32 AC 85 ms
22,156 KB
testcase_33 AC 85 ms
22,280 KB
testcase_34 AC 85 ms
24,152 KB
testcase_35 AC 84 ms
22,060 KB
testcase_36 AC 83 ms
22,116 KB
testcase_37 AC 85 ms
24,108 KB
testcase_38 AC 84 ms
20,052 KB
testcase_39 AC 82 ms
23,964 KB
testcase_40 AC 84 ms
22,056 KB
testcase_41 AC 89 ms
20,228 KB
testcase_42 AC 89 ms
24,224 KB
testcase_43 AC 83 ms
21,868 KB
testcase_44 AC 82 ms
23,860 KB
testcase_45 AC 83 ms
20,140 KB
testcase_46 AC 82 ms
21,884 KB
testcase_47 AC 83 ms
23,916 KB
testcase_48 AC 89 ms
20,056 KB
testcase_49 AC 85 ms
22,176 KB
testcase_50 AC 84 ms
22,284 KB
testcase_51 AC 83 ms
23,972 KB
testcase_52 AC 85 ms
24,208 KB
testcase_53 AC 74 ms
19,856 KB
testcase_54 AC 84 ms
24,164 KB
testcase_55 AC 83 ms
22,060 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.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
 
public class Program
{
    public void Proc() {
        Reader.IsDebug =false;
        int itemCount = int.Parse(Reader.ReadLine());
        Dictionary<Pos, bool> pList = new Dictionary<Pos, bool>();
        for(int i=0; i<itemCount; i++) {
            Match m = new Match(Reader.ReadLine());
            this.MatchList.Add(m);
            new Pos[] {m.P1, m.P2}.ToList().ForEach(a=>{
                if(pList.ContainsKey(a)) {
                    pList[a] = true;
                } else {
                    pList.Add(a, false);
                }
            });
        }
        pList.Where(a=>a.Value).Select(a=>a.Key).ToList().ForEach(a=>{
            PosList.Add(PosList.Count, a);
            KeyList.Add(a, KeyList.Count);
        });

        this.Target = this.MatchList.Where(a=>pList[a.P1] || pList[a.P2]).ToArray();
        bool ans = this.GetAns(0, new bool[PosList.Count]);
        Console.WriteLine(ans?"YES":"NO");
    }

    private Dictionary<int, Dictionary<string, bool>> dic = new Dictionary<int, Dictionary<string, bool>>();
    private bool GetAns(int idx, bool[] flags) {
        string key = string.Join(string.Empty, flags.Select(a=>a?1:0));
        if(!dic.ContainsKey(idx)) {
            dic.Add(idx, new Dictionary<string, bool>());
        }
        if(dic[idx].ContainsKey(key)) {
            return dic[idx][key];
        }

        if(idx >= Target.Count()) {
            return true;
        }

        Pos p1 = this.Target[idx].P1;
        Pos p2 = this.Target[idx].P2;

        if(KeyList.ContainsKey(p1)) {
            if(!flags[KeyList[p1]]) {
                bool[] sub = (bool[])flags.Clone();
                sub[KeyList[p1]] = true;
                bool ret = this.GetAns(idx+1, sub);
                if(ret) {
                    return true;
                }
            }
        } else {
            bool[] sub = (bool[])flags.Clone();
            bool ret = this.GetAns(idx+1, sub);
            if(ret) {
                return true;
            }
        }
        if(KeyList.ContainsKey(p2)) {
            if(!flags[KeyList[p2]]) {
                bool[] sub = (bool[])flags.Clone();
                sub[KeyList[p2]] = true;
                bool ret = this.GetAns(idx+1, sub);
                if(ret) {
                    return true;
                }
            }
        } else {
            bool[] sub = (bool[])flags.Clone();
            bool ret = this.GetAns(idx+1, sub);
            if(ret) {
                return true;
            }
        }
        dic[idx][key] = false;
        return false;
    }

    private Dictionary<Pos, int> KeyList = new Dictionary<Pos, int>();
    private Dictionary<int, Pos> PosList = new Dictionary<int, Pos>();

    private Match[] Target;
    private List<Match> MatchList = new List<Match>();
    private class Match {
        public Pos P1;
        public Pos P2;

        public Match(string init) {
            int[] inpt = init.Split(' ').Select(a=>int.Parse(a)).ToArray();
            this.P1 = new Pos(inpt[0], inpt[1]);
            this.P2 = new Pos(inpt[2], inpt[3]);
        }        
    }

    private struct Pos {
        public int Row;
        public int Col;
        public Pos(int r, int c) {
            this.Row = r;
            this.Col = c;
        }
    }


    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"



2
1 1 2 1
2 1 3 1


";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0