Beispiel #1
0
        static void Main(string[] args)
        {
            // ===== User Data Ingestion =====
            // Get dimensions of the grid.
            int[] gridDims = getIntegersInput(2);

            // Get the numbers in the grid. 0 represents a blank tile.
            int[][] gridContents = getGridContents(gridDims[0], gridDims[1]);

            // Get the number of capsules.
            int capsuleCount = getIntegersInput(1)[0];

            // Get the capsules.
            List <Capsule> capsules = getCapsules(capsuleCount);

            // ===== Grid Setup =====
            CapsuleGrid grid = new CapsuleGrid(gridContents, capsules);

            grid.solve();

            Console.WriteLine(grid);
        }
Beispiel #2
0
        // ===== Mutator Functions ======
        // Function to solve the grid.
        public void solve()
        {
            int[][]   grid           = this.gridState;
            int[][][] possibleStates = new int[grid.Length][][];
            for (int i = 0; i < grid.Length; i++)
            {
                possibleStates[i] = new int[grid[0].Length][];
            }

            // Iterate through each capsule.
            foreach (Capsule capsule in this.capsules)
            {
                foreach (int[] currentCell in capsule.getCells())
                {
                    if (grid[currentCell[0] - 1][currentCell[1] - 1] == 0)
                    {
                        possibleStates[currentCell[0] - 1][currentCell[1] - 1] = findPossibilities(currentCell, capsule);
                    }
                    else
                    {
                        possibleStates[currentCell[0] - 1][currentCell[1] - 1] = new int[100];
                    }
                }
            }

            // Chose the cell with the smallest number of possibilities.
            int[] possibleContents = possibleStates[0][0];
            int[] cell             = new int[] { 0, 0 };
            foreach (Capsule capsule in this.capsules)
            {
                foreach (int[] currentCell in capsule.getCells())
                {
                    int[] currentStates = possibleStates[currentCell[0] - 1][currentCell[1] - 1];
                    if (currentStates.Length < possibleContents.Length)
                    {
                        cell             = currentCell;
                        possibleContents = currentStates;
                    }
                }
            }

            // Change the possibility then recurse.
            foreach (int possibility in possibleContents)
            {
                int[][] newGridState = (int[][])this.gridState.Clone();

                newGridState[cell[0]][cell[1]] = possibility;

                CapsuleGrid newGrid = new CapsuleGrid(newGridState, this.capsules);

                if (newGrid.isSolved())
                {
                    Console.WriteLine("Break");
                    break;
                }

                newGrid.solve();
                if (newGrid.isGridCorrect())
                {
                    this.gridState = newGrid.getGridState();
                    break;
                }
            }
        }