Saturday, July 17, 2010

Program to solve N-queens problem by back-tracking

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace
ConsoleApplication4
{
    class Program
    {
        static int total = 0;//Variable which counts the number of possible arrangements.
        static void Main(string[] args)
        {
            int num = 8;
            int[,] board = new int[num,num];
            for(int i = 0; i < num; i++)
            {
                for(int j = 0; j < num; j++)
                {
                    board[i, j] = 0;
                }
            }
            queens(board, num, 0, 0, 0);
            Console.WriteLine("The number of possible arrangements is: " + total);
        }

        static void queens(int[,] board, int num, int count, int row, int j)
        {
            if(count == num)
            {
                total++;
                return;
            }
            if(j >= num && row >= num)
            {
                return;
            }
            else
            {
                j = 0;
                while(j < num && row < num)
                {
                    if(board[row, j] == 0)
                    {
                        int[,] prev = new int[num, num];
                        for (int m = 0; m < board.GetLength(0); m++)
                        {
                            for (int n = 0; n < board.GetLength(1); n++)
                            {
                                prev[m, n] = board[m, n];
                            }
                        }
                        board[row, j] = 1;
                        count++;
                        //Function to mark the squares where a queen cannot be placed.
                        queenplace(board, row, j);
                        queens(board, num, count, row + 1, j);

                        //Copying the board conditions of the previous call on returning.
                        for (int m = 0; m < num; m++)
                        {
                            for (int n = 0; n < num; n++)
                            {
                                board[m, n] = prev[m, n];
                            }
                        }
                        count--;
                        j++;
                    }
                    else
                    {
                        j++;
                    }
                }
            }
        }
        static void queenplace(int[,] board, int row, int j)
        {
            //Marking all squares of the row.
            for(int i = 0; i < board.GetLength(1); i++)
            {
                board[row, i] = 1;
            }
            //Marking all squares of the column.
            for(int i = 0; i < board.GetLength(0); i++)
            {
                board[i, j] = 1;
            }
            int rowpoint=row;
            int colpoint=j;
           //Marking the diagonal downwards towards the left.
            while(colpoint >= 0 && rowpoint < board.GetLength(0))
            {
                board[rowpoint, colpoint] = 1;
                rowpoint++;
                colpoint--;
            }
            rowpoint = row;
            colpoint = j;
            //Marking the diagonal downwards towards the right.
            while(colpoint < board.GetLength(1) && rowpoint < board.GetLength(0))
            {
                board[rowpoint, colpoint] = 1;
                rowpoint++;
                colpoint++;
            }
        }
    }
}

No comments:

Post a Comment