Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
This is what I have. It's a console version of the 8 queens and it can find the total solution (92). I am wondering how I am able to find the unique solutions(without rotations and symmetrys). I've tried everything and was wondering can help out just a teeny bit.
C#
using System;

namespace EightQueen
{
    public enum SIZE
    {
        size = 8
    }
    public class EightQueen
    {
        public static int count = 0;
        public static bool isValidMove(int[,] boardArr, int row, int col)
        {
            for (int i = 0; i < SIZE.size.GetHashCode(); i++)
                if (boardArr[row,i] == 1)
                    return false;

            for (int i = 0; i < SIZE.size.GetHashCode(); i++)
                if (boardArr[i,col] == 1)
                    return false;
            int tempCol = col + 1, tempRow = row ;

            for (int i = row + 1; i < SIZE.size.GetHashCode(); i++)
            {
                if (tempCol < SIZE.size.GetHashCode())
                {
                    if (boardArr[i,tempCol] == 1)
                        return false;
                    else
                        tempCol++;
                }
                else
                    break;
            }
            tempCol = col - 1;
            for (int i = row - 1; i >= 0; i--)
            {
                if (tempCol >= 0)
                {
                    if (boardArr[i,tempCol] == 1)
                        return false;
                    else
                        tempCol --;
                }
                else
                    break;
            }
            tempCol = col - 1;
            for (int i = row + 1; i < SIZE.size.GetHashCode(); i++)
            {
                if (tempCol >= 0)
                {
                    if (boardArr[i, tempCol] == 1)
                        return false;
                    else
                        tempCol--;
                }
                else
                    break;
            }
            tempCol = col + 1;
            for (int i = row - 1; i >= 0; i--)
            {
                if (tempCol < SIZE.size.GetHashCode())
                {
                    if (boardArr[i, tempCol] == 1)
                        return false;
                    else
                        tempCol++;
                }
                else
                    break;
            }

            return true;
        }

        public static void ShouldPrintBoard(int[,] boardArr)
        {
            int count = 0;
            for (int i = 0; i < SIZE.size.GetHashCode(); i++)
                for(int j = 0; j < SIZE.size.GetHashCode() ;j++)
                    if (boardArr[i,j] == 1)
                        count++;
            if (count >= 8)
                PrintBoard(boardArr);
        }
        public static void PrintBoard(int[,] boardArray)
        {
            for (int i = 0; i < SIZE.size.GetHashCode(); i++)
            {
                for (int j = 0; j < SIZE.size.GetHashCode(); j++)
                    Console.Write("{0} ", boardArray[i, j]);
                Console.WriteLine();
            }
            count++;
            Console.WriteLine();
            //Console.ReadKey();
        }
        public static void PermuteBoard(int[,] boardArr, int row, int col)
        {
            for (int i = row; i < SIZE.size.GetHashCode(); i++)
            {
                for (int j = 0; j < SIZE.size.GetHashCode(); j++)
                {
                    if (isValidMove(boardArr, i, j))
                    {
                        boardArr[i, j] = 1;
                        ShouldPrintBoard(boardArr);
                        PermuteBoard(boardArr, i + 1, j + 1);
                        boardArr[i, j] = 0;
                    }

                }
            }
        }
        static void Main(string[] args)
        {
            int [,]boardArr = new int[SIZE.size.GetHashCode(),SIZE.size.GetHashCode()];
            for(int i = 0 ; i < SIZE.size.GetHashCode() ; i++)
                for(int j = 0 ; j < SIZE.size.GetHashCode() ; j++)
                    boardArr[i,j] = 0;
            PermuteBoard(boardArr, 0, 0);

            Console.WriteLine("Total Solutions {0}", count);
            Console.ReadKey();
        }
    }
}
Posted

 
Share this answer
 
Comments
Nikil S 12-Dec-11 18:01pm    
Good one, my 5!

A nice template, saves lot of time to answer. Surely will help in future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900