Exemplo n.º 1
0
        public InputFile(string filePath)
        {
            using (var file = File.OpenText(filePath))
            {
                var testCaseCount = int.Parse(file.ReadLine());

                testCases = new List<TestCase>(testCaseCount);

                for (var i = 0; i < testCaseCount; i++)
                {
                    var kn = ReadInts(file);
                    var keys = ReadInts(file);

                    var N = kn[1];
                    var chests = new Chest[N];

                    for (var j = 0; j < N; j++)
                    {
                        var tokens = ReadInts(file);
                        chests[j] = new Chest(j + 1, tokens[0], tokens.Skip(2));
                    }

                    testCases.Add(new TestCase(keys, chests));
                }
            }
        }
Exemplo n.º 2
0
        public IList<int> GetSolution()
        {
            var chest = new Chest(0, this.keys.Keys.ToList());
            chest.Open();
            this.chests.Insert(0, chest);
            var count = this.chests.Count;
            var result = new int[count];

            int pos = 1;

            if (GetSolution(count, pos, result))
                return result;

            return null;
        }
Exemplo n.º 3
0
        public Strategy OpenChest(Chest chest)
        {
            var keyIndex = Array.IndexOf(keysInHand, chest.RequiredKey);
            Debug.Assert(keyIndex >= 0);

            var newKeysInHand =
                keysInHand
                .Where((key,idx) => idx != keyIndex)
                .Concat(chest.ContainedKeys);
            Debug.Assert(newKeysInHand.Count() == keysInHand.Count() - 1 + chest.ContainedKeys.Count());

            var newOpenedChests =
                openedChests.Concat(Enumerable.Repeat(chest, 1));
            Debug.Assert(newOpenedChests.Count() == openedChests.Count() + 1);

            var newClosedChests =
                closedChests.Where(x=>x != chest);
            Debug.Assert(newClosedChests.Count() == closedChests.Count() - 1);

            return new Strategy(newKeysInHand, newOpenedChests, newClosedChests);
        }
Exemplo n.º 4
0
        private void RecursivelyOpenChests(HashSet<Chest> scannedChests, Chest chest)
        {
            if (scannedChests.Contains(chest)) return;

            scannedChests.Add(chest);

            var openableChests = ClosedChests.Where(x=>chest.ContainedKeys.Contains(x.RequiredKey));

            foreach (var nextChest in openableChests)
                RecursivelyOpenChests(scannedChests, nextChest);
        }
Exemplo n.º 5
0
 public void AddChest(Chest chest)
 {
     this.chests.Add(chest);
     chest.Opener = -1;
     this.availablekeys = this.availablekeys.Add(chest.Keys);
 }
Exemplo n.º 6
0
 public TestCase(int[] keys, Chest[] chests)
 {
     this.keys = keys;
     this.chests = chests;
 }
Exemplo n.º 7
0
        public static List <Problem> ParseInput(string[] lines)
        {
            int caseCount = 0;

            List <Problem> problems      = new List <Problem>();
            Problem        problem       = new Problem();
            PARSE_STATE    state         = PARSE_STATE.P_BEGIN;
            int            startKeyCount = 0;
            int            chestCount    = 0;
            int            chestNumber   = 1;

            for (int index = 0; index < lines.Length; index++)
            {
                string line = lines[index];

                if (index == 0)
                {
                    if (!Int32.TryParse(line, out caseCount))
                    {
                        Console.WriteLine("Invalid line {0}:{1}", index, line);
                        break;
                    }
                }
                else if (state == PARSE_STATE.P_BEGIN)
                {
                    string[] items = line.Split(' ');
                    startKeyCount = int.Parse(items[0]);
                    chestCount    = int.Parse(items[1]);
                    state         = PARSE_STATE.P_READ_START;
                }
                else if (state == PARSE_STATE.P_READ_START)
                {
                    string[] items = line.Split(' ');
                    foreach (var item in items)
                    {
                        int key = int.Parse(item);
                        if (problem.KeyStart.ContainsKey(key))
                        {
                            ++problem.KeyStart[key];
                        }
                        else
                        {
                            problem.KeyStart[key] = 1;
                        }
                    }
                    state = PARSE_STATE.P_READ_CHEST;
                }
                else if (state == PARSE_STATE.P_READ_CHEST)
                {
                    Chest chest = new Chest();
                    chest.ChestNumber = chestNumber;
                    ++chestNumber;
                    string[] items = line.Split(' ');
                    for (int i = 0; i < items.Length; i++)
                    {
                        int key = int.Parse(items[i]);

                        if (i == 0)
                        {
                            chest.KeysToOpen = key;
                        }
                        else if (i > 1)
                        {
                            if (chest.KeysInside.ContainsKey(key))
                            {
                                ++chest.KeysInside[key];
                            }
                            else
                            {
                                chest.KeysInside[key] = 1;
                            }
                        }
                    }

                    problem.Chests.Add(chest.ChestNumber, chest);

                    if (problem.Chests.Count == chestCount)
                    {
                        problems.Add(problem);
                        problem       = new Problem();
                        state         = PARSE_STATE.P_BEGIN;
                        startKeyCount = 0;
                        chestCount    = 0;
                        chestNumber   = 1;
                    }
                }
            }

            if (problems.Count != caseCount)
            {
                Console.WriteLine("Invalid case count");
            }

            return(problems);
        }
Exemplo n.º 8
0
        private static void Revert(Dictionary <int, int> availableKeys, List <int> openSequence, Chest chest, Dictionary <int, int> retrievedKey)
        {
            openSequence.RemoveAt(openSequence.Count - 1);
            if (availableKeys.ContainsKey(chest.KeysToOpen))
            {
                ++availableKeys[chest.KeysToOpen];
            }
            else
            {
                availableKeys[chest.KeysToOpen] = 1;
            }

            foreach (var item in retrievedKey)
            {
                availableKeys[item.Key] -= item.Value;
                if (availableKeys[item.Key] == 0)
                {
                    availableKeys.Remove(item.Key);
                }
            }
        }