N queens puzzle

N queens puzzle

Sample program

The following is a C Coded by "Tamajit Chakraborty" It can finds one solution to the N queens problem.

#include<stdio.h>
#include<conio.h>
 
#define insertQ 1
#define full 0
#define cons 5
void main()
{
        int n,i,j,a[100][100],k,x=0,c;
        clrscr();
        printf("Enter The Number of QUEENS: ");
        scanf("%d",&n);
        do
        {
                c=n;
                for(i=0;i<n;i++)
                        for(j=0;j<n;j++)
                                a[i][j]=cons;
                a[0][x]=insertQ;
                c--;
                if(x>0)
                {
                        a[1][x-1]=full;
                        for(j=x-1;j>=0;j--)
                                a[0][j]=full;
                }
                for(i=1;i<n;i++)
                        a[i][x]=full;
                a[1][x+1]=full;
                for(j=x+1;j<n;j++)
                        a[0][j]=full;
                for(i=1;i<n;i++)
                {
                        for(j=0;j<n;j++)
                        {
                                if(a[i][j]==cons)
                                {
                                        a[i][j]=insertQ;
                                        c--;
                                        for(k=1;i+k<n;k++)
                                                a[i+k][j]=full;
                                        for(k=1;j+k<n;k++)
                                                a[i][j+k]=full;
                                        if(j>0)
                                                a[i+1][j-1]=full;
                                        if(i+1<n && j+1<n)
                                                a[i+1][j+1]=full;
                                }
                        }
                }
                x++;
 
        }while(c!=0);
        for(i=0;i<n;i++)
        {
                for(j=0;j<n;j++)
                {
                        if(a[i][j]!=full)
                                printf("Q ");
                        else
                                printf("X ");
                }
                printf("\n");
        }
        getch();
}



Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Eight queens puzzle — a b c d e f g h …   Wikipedia

  • Eight queens puzzle solutions — This article shows algorithmic solutions to the eight queens puzzle implemented in various computer programming languages. For specific outcomes of these algorithms, see the main eight queens puzzle article.Board representations* The naive… …   Wikipedia

  • Puzzle — For other uses, see Puzzle (disambiguation). Puzzle solving redirects here. For the concept in Thomas Kuhn s philosophy of science, see normal science. Part of a series on Puzzles …   Wikipedia

  • List of puzzle topics — This is a list of puzzle topics, by Wikipedia page.See also: * List of impossible puzzles * List of puzzle based computer and video games * List of game topics.* Acrostic * Anagram * Back from the klondike * Burr puzzle * Chess problem * Chess… …   Wikipedia

  • Chess puzzle — A chess puzzle is a puzzle in which knowledge of the pieces and rules of chess is used to solve logically a chess related problem. The longstanding popularity of chess has paved the way for a rich tradition of such chess related puzzles and… …   Wikipedia

  • Mathematical puzzle — Mathematical puzzles make up an integral part of recreational mathematics. They have specific rules as do multiplayer games, but they do not usually involve competition between two or more players. Instead, to solve such a puzzle, the solver must …   Wikipedia

  • Inner Tube (The King of Queens) — Infobox Television episode Title = Inner Tube Series = The King of Queens Season = 3 Episode = 17 Writer = Kevin James, Rock Reuben Director = Rob Schiller Production = 317 Airdate = February 26, 2001 Episode list = List of The King of Queens… …   Wikipedia

  • Magic square — In recreational mathematics, a magic square of order n is an arrangement of n2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant.[1] A normal magic… …   Wikipedia

  • Mathematical chess problem — is a mathematical problem which is formulated using chessboard or chess pieces. These problems belong to recreational mathematics. The most known problems of this kind are Eight queens puzzle or Knight s Tour problems, which have connection to… …   Wikipedia

  • Min-conflicts algorithm — The min conflicts algorithm is a search algorithm to solve constraint satisfaction problems (CSP problems). It assigns random values to all the variables of a CSP. Then it selects randomly a variable, whose value conflicts with any constraint of… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”