Saturday, July 17, 2010

Power Set



Printing all the
possible combinations of a set of elements



using System;



using
System.Collections;



using
System.Linq;



using
System.Text;



 



namespace
ConsoleApplication3



{



    class Program



    {



        static int count = 0;//Variable
which counts the number of combinations printed.



        static void Main(string[]
args)



        {



            ArrayList
list = new ArrayList();



            string
input = "abcde";



            char[]
arr = new char[input.Length];



            arr = input.ToCharArray();



            //int[]
arr = { 1, 2, 3, 4, 5 };



            combine(arr,list,0);



            Console.WriteLine("The total number of combinations is: "
+ count);



        }



        //Change
char[]arr to int[]arr to use the function for an int array.



        static void combine(char[]
arr, ArrayList list, int index)



        {



            if
(index == arr.Length)



            {



                return;



            }



            else



            {



                while
(index < arr.Length)



                {



                    list.Add(arr[index]);



                    //Printing
format



                    Console.Write("{");



                    for
(int i = 0; i < list.Count; i++)



                    {



                        if (i !=
list.Count - 1)



                        {



                            Console.Write(list[i] + ",");



                        }



                        else



                        {



                            Console.Write(list[i]);



                        }



                    }



                    Console.Write("}");



                    Console.WriteLine();



                    //Printing
format.



                    count++;



                    combine(arr, list, index +
1);



                    //Removing
the last element of the present list on returning.



                    list.RemoveAt(list.Count -
1);



                    index++;



                }



            }



        }



    }



}



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++;
            }
        }
    }
}