Example #1
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            string resultString = "";

            int[,] sudokuText = new int[9, 9] {
                { 5, 3, 0, 0, 7, 0, 0, 0, 0 },
                { 6, 0, 0, 1, 9, 5, 0, 0, 0 },
                { 0, 9, 8, 0, 0, 0, 0, 6, 0 },
                { 8, 0, 0, 0, 6, 0, 0, 0, 3 },
                { 4, 0, 0, 8, 0, 3, 0, 0, 1 },
                { 7, 0, 0, 0, 2, 0, 0, 0, 6 },
                { 0, 6, 0, 0, 0, 0, 2, 8, 0 },
                { 0, 0, 0, 4, 1, 9, 0, 0, 5 },
                { 0, 0, 0, 0, 8, 0, 0, 7, 9 }
            };

            int[,] sudokuTextUnsolvable = new int[9, 9] {
                { 5, 3, 2, 0, 7, 0, 0, 0, 0 },
                { 6, 0, 0, 1, 9, 5, 0, 0, 0 },
                { 0, 9, 8, 0, 0, 0, 0, 6, 0 },
                { 8, 0, 0, 0, 6, 0, 0, 0, 3 },
                { 4, 0, 0, 8, 0, 3, 0, 0, 1 },
                { 7, 0, 0, 0, 2, 0, 0, 0, 6 },
                { 0, 6, 0, 0, 0, 0, 2, 8, 0 },
                { 0, 0, 0, 4, 1, 9, 0, 0, 5 },
                { 0, 0, 0, 0, 8, 0, 0, 7, 9 }
            };

            SudokuBoard bTest = new SudokuBoard(sudokuText);

            resultString += "## TEST 1 - Solvable\n";
            resultString += bTest.toString() + "\n";

            // System.Console.WriteLine(bTest.toString());

            var result = BackTracking.BacktrackingSearch(bTest);

            resultString += "## RESULT 1\n";

            resultString += result.toString() + "\n";

            // System.Console.WriteLine(result.toString());
            SudokuBoard buTest = new SudokuBoard(sudokuTextUnsolvable);

            resultString += "## TEST 2 - Unsolvable\n";

            resultString += buTest.toString() + "\n";

            // System.Console.WriteLine(bTest.toString());

            result        = BackTracking.BacktrackingSearch(buTest);
            resultString += "## RESULT 2\n";

            resultString += result.toString() + "\n";

            await File.WriteAllTextAsync("Result.txt", resultString);

            // System.Console.WriteLine(resultString);
            // System.Console.WriteLine(result.toString());
        }
Example #2
0
 private void SolveProblem()
 {
     if (_problemType == 1)
     {
         BackTracking bct = new BackTracking();
         _solutionsFuto = bct.FutoSolver(_box.ProblemFuto);
         _iterations    = bct.Iterations;
     }
     else if (_problemType == 2)
     {
     }
 }
Example #3
0
        public ActionResult <SudokuBoardDto> Solution(SudokuBoardDto sbDto)
        {
            var result = new BackTracking().BacktrackingSearch(
                new SudokuBoard(sbDto.Board)
                );

            if (result.Failed)
            {
                return(null);
            }

            return(Ok(new SudokuBoardDto(result)));
        }
 public void TestExist()
 {
     //char[][] board = new char[3][]
     //{
     //    new char[] { 'A','B','C','E' },
     //    new char[] { 'S','F','C','S' },
     //    new char[] { 'A','D','E','E' },
     //};
     char[][] board = new char[][]
     {
         new char[] { 'a', 'a' }
     };
     BackTracking.Exist(board, "aa");
 }
Example #5
0
		private static string LCS(string[] list1, string[] list2) 
		{
			int m=list1.Length ;
			int n=list2.Length ;
			
			int[ , ] lcs=new int[m+1, n+1];
			BackTracking[ , ] backTracer=new BackTracking[m+1, n+1];
			int[ , ] w=new int[m+1, n+1];
			int i, j;
			
			for(i=0; i <= m; ++i) 
			{
				lcs[i,0] = 0;
				backTracer[i,0]=BackTracking.UP;
				
			}
			for(j= 0; j <= n; ++j) 
			{
				lcs[0,j]=0;
				backTracer[0,j]=BackTracking.LEFT;				
			}

			for(i =1; i <= m; ++i) 
			{
				for(j=1; j <= n; ++j) 
				{ 
					if( list1[i-1].Equals(list2[j-1]) ) 
					{
						int k = w[i-1, j-1];
						//lcs[i,j] = lcs[i-1,j-1] + 1;
						lcs[i,j]=lcs[i-1,j-1] + ConsecutiveMeasure(k+1) - ConsecutiveMeasure(k)  ;
						backTracer[i,j] = BackTracking.UP_AND_LEFT;
						w[i,j] = k+1;						
					}
					else 
					{
						lcs[i,j] = lcs[i-1,j-1];
						backTracer [i,j] = BackTracking.NEITHER;
					}

					if( lcs[i-1,j] >= lcs[i,j] ) 
					{	
						lcs[i,j] = lcs[i-1,j];
						backTracer[i,j] = BackTracking.UP;
						w[i,j] = 0;
					}

					if( lcs[i,j-1] >= lcs[i,j] ) 
					{
						lcs[i,j] = lcs[i,j-1];
						backTracer [i,j] = BackTracking.LEFT;
						w[i,j] = 0;
					}
				}
			}
			
			i=m; 
			j=n;
			
			string subseq="";
			int p=lcs[i,j];

			//trace the backtracking matrix.
			while( i > 0 || j > 0 ) 
			{
				if( backTracer[i,j] == BackTracking.UP_AND_LEFT ) 
				{
					i--;
					j--;
					subseq = list1[i] + subseq;
					Trace.WriteLine(i + " " + list1[i] + " " + j) ;
				}
	
				else if( backTracer[i,j] == BackTracking.UP ) 
				{
					i--;
				}
	
				else if( backTracer[i,j] == BackTracking.LEFT ) 
				{
					j--;
				}
			}
			
			
			return subseq ;
		}
Example #6
0
        private static string LCS(string[] list1, string[] list2)
        {
            int m = list1.Length;
            int n = list2.Length;

            int[ , ] lcs = new int[m + 1, n + 1];
            BackTracking[ , ] backTracer = new BackTracking[m + 1, n + 1];
            int[ , ] w = new int[m + 1, n + 1];
            int i, j;

            for (i = 0; i <= m; ++i)
            {
                lcs[i, 0]        = 0;
                backTracer[i, 0] = BackTracking.UP;
            }
            for (j = 0; j <= n; ++j)
            {
                lcs[0, j]        = 0;
                backTracer[0, j] = BackTracking.LEFT;
            }

            for (i = 1; i <= m; ++i)
            {
                for (j = 1; j <= n; ++j)
                {
                    if (list1[i - 1].Equals(list2[j - 1]))
                    {
                        int k = w[i - 1, j - 1];
                        //lcs[i,j] = lcs[i-1,j-1] + 1;
                        lcs[i, j]        = lcs[i - 1, j - 1] + ConsecutiveMeasure(k + 1) - ConsecutiveMeasure(k);
                        backTracer[i, j] = BackTracking.UP_AND_LEFT;
                        w[i, j]          = k + 1;
                    }
                    else
                    {
                        lcs[i, j]         = lcs[i - 1, j - 1];
                        backTracer [i, j] = BackTracking.NEITHER;
                    }

                    if (lcs[i - 1, j] >= lcs[i, j])
                    {
                        lcs[i, j]        = lcs[i - 1, j];
                        backTracer[i, j] = BackTracking.UP;
                        w[i, j]          = 0;
                    }

                    if (lcs[i, j - 1] >= lcs[i, j])
                    {
                        lcs[i, j]         = lcs[i, j - 1];
                        backTracer [i, j] = BackTracking.LEFT;
                        w[i, j]           = 0;
                    }
                }
            }

            i = m;
            j = n;

            string subseq = "";
            int    p      = lcs[i, j];

            //trace the backtracking matrix.
            while (i > 0 || j > 0)
            {
                if (backTracer[i, j] == BackTracking.UP_AND_LEFT)
                {
                    i--;
                    j--;
                    subseq = list1[i] + subseq;
                    Trace.WriteLine(i + " " + list1[i] + " " + j);
                }

                else if (backTracer[i, j] == BackTracking.UP)
                {
                    i--;
                }

                else if (backTracer[i, j] == BackTracking.LEFT)
                {
                    j--;
                }
            }


            return(subseq);
        }
 public void TestPermute()
 {
     BackTracking.Permute(new int[] { 1, 2, 3 });
 }
 public void TestLetterCombinations()
 {
     BackTracking.LetterCombinations("23");
 }