예제 #1
0
        private static bool Search(Queens queens, int N)
        {
            int col, row;

            for (int i = 0; i < N * K; i++)
            {
                if (queens.IsValid())
                {
                    return(true);
                }
                col = queens.GetColWithMaxConflicts();
                if (col == -1)
                {
                    break;
                }

                row = queens.GetRowWithMinConflicts(col);
                if (row == -1)
                {
                    break;
                }

                queens.MooveQueen(col, row);
            }
            return(queens.IsValid());
        }
예제 #2
0
        private static bool UnoptimizedSearch(Queens queens, int N)
        {
            queens.Init();
            int col, row;

            for (int i = 0; i < N * K; i++)
            {
                if (N <= 50)
                {
                    queens.PrintQueens();
                }

                if (queens.IsValid())
                {
                    return(true);
                }
                col = GetColWithMaxConflicts(queens.queens);
                if (col == -1)
                {
                    break;
                }

                row = GetRowWithMinConflicts(queens.queens, col);
                if (row == -1)
                {
                    break;
                }

                queens.MooveQueen(col, row);
            }
            return(queens.IsValid());
        }
예제 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            int N     = Int32.Parse(Console.ReadLine());
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            var queens = new Queens(N);

            Console.WriteLine($"Init Time: {watch.ElapsedMilliseconds} ms");
            watch.Stop();
            watch.Reset();

            watch.Start();
            while (!Search(queens, N))
            {
                ;
            }
            watch.Stop();

            Console.WriteLine($"Search Time: {watch.ElapsedMilliseconds} ms");

            if (N <= 50)
            {
                queens.PrintQueens();
            }
            Console.WriteLine("Done");
        }