arrays - Recursive Method For Checking All Possible Values (C) -


i'm writing program reads in integer value (n), , creates chess table dimensions (nxn). have see if there way place n queens on board in such way none of them attack each other (for of unfamiliar chess, queens can attack piece in same column or row them, piece sits on diagonal passes through queen). values stored in array (int board[n]), set -1. value in board[0] correspond row queen in column 0 located at. initially, values set @ -1, means there no piece in said row.

i'm trying find way in pass every possible set of coordinates (essentially every possible array of length n, , values can range between 0 , n-1) through method checks see if valid way lay out pieces, , if there layout works, must pass array method visualizes it, so:

* * q * q * * * * * * q * q * * 

the methods checking , outputting set , working, need figure out how generate possible permutations of array stores coordinates.

edit: here code far (updated of 2pm, december 6th):

#include <stdio.h> #include <stdlib.h> int permutations (int board[],int n, int counter, int index); int check (int board[], int n); int printout (int board[], int n, int isvalid); int main() {   int n, board[n];   while (1==scanf("%d", &n) && n > 0) {       int board[] = {-1};       int isvalid = permutations(board,n, 0, 0);        printout(board, n, isvalid); }   return 0; } int permutations(int board[],int n, int counter, int index){ board[index] = counter; int max = n-1; if ((check(board, n) == 1) && (index == max)){ return 1; } if (check(board, n) == 1){ permutations(board, n, 0, ++index); } else if (check(board, n) == 0){ counter++; } if (counter == n){ return 0; }   } int check(int board[], int n){ int i,j; int isvalid = 1; (i=0; i<n && isvalid; ++i) {       if (board[i]==-1) continue;       (j=i+1; j<n && isvalid; ++j) {     if (board[j]==-1) continue;     if ( board[i] == board[j] ||              board[i]-board[j] == i-j ||              board[i]-board[j] == j-i )       isvalid = 0;       }     } return isvalid;} int printout(int board[], int n, int isvalid){ int i,j;     putchar('\n');     (i=n-1; i>=0; --i) {       (j=0; j<n; ++j) {     if (j>0) putchar(' ');     putchar( board[j]==i ? 'q' : '.' );       }       putchar('\n');     }     puts( isvalid ? "valid configuration" : "invalid configuration" );     return 0;     } 

hint homework (recursion):

start empty board , call recursive routine. iterate on board, trying put queen on it. if attacked continue. otherwise call board (one more queen now). iterate on board, trying put queen on it. if attacked continue. otherwise call yourself...

if have reached 6 queens, return "success" , unwind recursion.

if @ point cannot place queen on board, return "failure", remove queen , continue iteration called yourself.

this "brute force" approach.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -