Skip to main content

Program in C to Find 'N' Queens on an "N x N" Board

 A.K.A - "N-Queens Problem"
(Note: I computed till 15. N-Queens is only defined for  N>= 4)
 Manipulate the constant named "MAX"
 
Program:
 
#include<stdio.h>
#include<conio.h>
#define MAX 8
int q[MAX] = { 0 };

void printem()
{
        int i, j,k;

        for (i = 0; i < MAX; i++) {

        printf("\n\t");

        for (j = q[i], k = MAX - j; j > 0; j--) {

            printf("-");
        }

        printf("X");

        for (; k > 0; k--) {

            printf("-");
        }

 }//Outer for ends
}

int dist(int x, int y)
{

 if (x > y)
        return (x - y);
 return (y - x);
}

int match(int i, int j)
{

    int k, xi, xj;
    for (k = 0; k < i; k++) {
      
       xi = k;
       xj = q[k];

       if (xi == i || xj == j)
            return (0);
       if (dist(i, xi) == dist(j, xj))
            return (0);
    }
    for (; k < MAX; k++) {
        q[k] = -1;
    }
    return (1);
}

int main()
{

    int i = 0, k, ci, cj;
    clrscr();
    for (k = 0; k < MAX;) {

       if (i >= MAX)  // My Version of Back tracking
       {
           printf("nBackTracked... %dth Queen", k + 1);
           k--;
           while ((q[k] + 1) >= MAX)
                 k--;
           i = q[k] + 1;
       }

       ci = i++;

       if (match(k, ci) == 1) //k - x axis. ci - y axis.
       {
            i = 0;
            q[k] = ci;
            k++;
       }
    } // for loop ends
    printf("nThe Queens: ");
    for (i = 0; i < MAX; i++)
       printf("n%d: %d", i + 1, q[i]);

    //printem(); //remove comment to see how it looks on the board
    getch();
}

Comments

Popular posts from this blog

4. Lex and Yacc Program to detect errors in a 'C' Language Program

Lex and Yacc Program to detect errors in a 'C' Language Program   Lex Code : %{ #include"y.tab.h" #include<stdio.h> int LineNo = 1 ; %} identifier [ a - zA - Z ][ _a - zA - Z0 - 9 ]* number [ 0 - 9 ]+|([ 0 - 9 ]*\.[ 0 - 9 ]+) %% main \(\) return MAIN ; if return IF ; else return ELSE ; while return WHILE ; int | char | flaot return TYPE ; { identifier } return VAR ; { number } return NUM ; \> | \< | \<= | \>= | == return RELOP ; [\ t ] ; [\ n ] LineNo ++; . return yytext [ 0 ]; %% Yacc Code : %{ #include<string.h> #include<stdio.h> extern int LineNo ; int errno = 0 ; %} % token NUM VAR RELOP % token MAIN IF ELSE WHILE TYPE % left '-' '+' % left '*' '/' %% PROGRAM : MAIN BLOCK ; BLOCK : '{' CODE '}' ; CODE : BLOCK | STATEMENT CODE | STATEMENT ; STATEMENT : DECST ';' | DECST { printf ( "\nLine number %d...

Selenium + Python + UnexpectedAlertPresentException: Dealing with annoying alerts

Handling  UnexpectedAlertPresentException   Alerts who hates them? I Do!  Who doesn't hate an annoying alert causing your tests / scraping job to fail? I must say they are pretty much on point on the Unexpected part!  Fortunately, there are easy ways to mitigate the issue. 1. Disable alerts completely: driver . execute_script( 'window.alert = function(){};' ); execute this script just before where you anticipate the alert and you're golden. 2. You want to see the alert text but not disturb the execution flow. driver . execute_script( 'window.alert = console.info;' ); Now the alerts have been redirected to the console and you don't have to worry about them. (Unless you have to - then you'd have to monitor the console) 3. You know exactly when it comes and want to accept the alert and move on. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from selenium import webdriver from selenium.webdriver.s...

2. Lex program that detects statement type i.e. Simple or Compound

Lex program that detects statement type i.e. Simple or Compound Note: Only AND | OR | BUT conjunctions are supported. Program: % option noyywrap %{ char test = 's' ; %} %% ( "" [ aA ][ nN ][ dD ] "" )|( "" [ oO ][ rR ] "" )|( "" [ bB ][ uU ][ tT ] "" ) { test = 'c' ;} . {;} \ n return 0 ; %% main () { yylex (); if ( test == 's' ) printf ( "\n Its a simple sentence" ); else if ( test == 'c' ) printf ( "\n This is compound sentence" ); }