結果

問題 No.483 マッチ並べ
ユーザー 14番
提出日時 2017-02-12 03:17:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 4,050 bytes
コンパイル時間 1,165 ms
コンパイル使用メモリ 110,336 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2024-12-29 16:44:53
合計ジャッジ時間 6,226 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます
コンパイルメッセージ
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