Difference between revisions of "N-Queens on the Arduino"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- * Make sure you set the baud rate to 9600 in your IDE. * Source: <br /> ::<source lang="C"> // This code taken from // http://www.c4learn.com/c-programs/c-progran...")
 
 
Line 8: Line 8:
 
// http://www.c4learn.com/c-programs/c-progran-to-implement-n-queens-problem.html
 
// http://www.c4learn.com/c-programs/c-progran-to-implement-n-queens-problem.html
 
// and adapted (minimally) by D. Thiebaut
 
// and adapted (minimally) by D. Thiebaut
// Change the variable n in setup() to reflect the number of queens, and run the program.
+
// Change the variable n in setup() to reflect the number of queens, and make sure the
 +
// dimension of char[][] a is set larger than n.
 +
// run the program.
 
// Set the baud rate to 9600 in the IDE.
 
// Set the baud rate to 9600 in the IDE.
 
// The program outputs two number in the Serial console:
 
// The program outputs two number in the Serial console:

Latest revision as of 11:40, 20 April 2016

--D. Thiebaut (talk) 11:38, 20 April 2016 (EDT)


  • Make sure you set the baud rate to 9600 in your IDE.
  • Source:


// This code taken from 
// http://www.c4learn.com/c-programs/c-progran-to-implement-n-queens-problem.html
// and adapted (minimally) by D. Thiebaut
// Change the variable n in setup() to reflect the number of queens, and make sure the
// dimension of char[][] a is set larger than n.
// run the program.
// Set the baud rate to 9600 in the IDE.
// The program outputs two number in the Serial console:
// n
// ms
// where n is the number of queens, and ms is the elapsed time for the computation of
// the placement of N queens, expressed in milliseconds.

#include<stdio.h>
#include<math.h>

char a[20][20];
int n;

void printmatrix() {
 int i, j;
 return;
 printf("\n");

 for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
       printf("%c\t", a[i][j]);
    printf("\n\n");
 }
}

int getmarkedcol(int row) {
 int i;
 for (i = 0; i < n; i++)
    if (a[row][i] == 'Q') {
       return (i);
       break;
    }
}

int feasible(int row, int col) {
 int i, tcol;
 for (i = 0; i < n; i++) {
    tcol = getmarkedcol(i);
    if (col == tcol || abs(row - i) == abs(col - tcol))
       return 0;
 }
 return 1;
}

void nqueen(int row) {
 int i, j;
 if (row < n) {
    for (i = 0; i < n; i++) {
       if (feasible(row, i)) {
          a[row][i] = 'Q';
          nqueen(row + 1);
          a[row][i] = '.';
       }
    }
 } else {
    //Serial.print( F("Done!\n" ) );
 }
}

void setup() {
 int i, j;
 unsigned long StartTime;
 unsigned long CurrentTime;
 unsigned long ElapsedTime;
 Serial.begin( 9600 );

 n=10;
 Serial.print( n );
 Serial.print( F("\n") );
 for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
       a[i][j] = '.';

 StartTime = millis();
 nqueen(0);
 CurrentTime = millis();
 ElapsedTime = CurrentTime - StartTime;
 Serial.print( ElapsedTime );
}

void loop() {
// put your main code here, to run repeatedly:

}