Add to Favorites    Make Home Page 4080 Online
 Language Categories
 Source Codes Home Project Ideas New! Interview Questions FAQs Home ASP Home ASP Source Codes ASP Script Directory New! ASP .Net Script Directory New! ASP Interview Questions ASP FAQs ASP How Tos C Home C Source Codes C Script Directory New! C Interview Questions C FAQs C How Tos C++ Home C++ Source Codes C++ Script Directory New! C++ Interview Questions C++ FAQs C++ How Tos Java Home Java Source Codes Java Directory New! Java Interview Questions Java FAQs Java How Tos JavaScript Home JavaScript Directory New! JavaScript Source Codes JavaScript FAQs JavaScript How Tos COBOL Home COBOL Source Codes COBOL FAQs COBOL How Tos Pascal Home Pascal Source Codes Pascal FAQs Pascal How Tos PHP Script Directory New! Python Script Directory New! Perl & CGI Script Directory New! Flash Script Directory New! CFML Script Directory New! Remotely Hosted Scripts New! Tools & Utilities Directory New! XML Script Directory New! Best Programmers Amit Mathur Vishal Bhardwaj Deepesh Jain Vyom NetWork
 Our Services

Home » C Home » Beginners / Lab Assignments Home » N-Queen's Problem

A D V E R T I S E M E N T

Search Projects & Source Codes:

 Title N-Queen's Problem Author Biswajit Debnath Author Email biswajitdebnathrbsc [at] yahoo.co.in Description This is NQueens' problem. Here n queens will have to be placedin (n x n) matrix in such a way that none will meet any other wile movinghorizontally, vertically or along diagonal with respect to its position.The program is able to take any input, but for better output try it withnot more than 23. Category C » Beginners / Lab Assignments Hits 367940 Code Select and Copy the Code /* @@ Problem : This is n-Queen problem. @@ Description : To place n queens in (n x n) matrix such that no queen meets another while moving along the row, col or diag- onal with respect to to its position. @@ Done by : Biswajit Debnath. @@ University : RajaBazar Science College(Calcutta University), Kolkata @@ E-mail : biswajitdebnathrbsc@yahoo.co.in @@ Mobile no : 9433 217955 @@ Request : If you have any sugestion or quiry, please do not hesi- tate to mail me. */ #include #include #include #include #include void check(int, int, char [100][100]); void print(char [100][100]); int no_of_queens, queen = 2, flagrow = 0, flagcol = 0; int count = 1; char ch, response_row, response_col; int main(void) { int row, col, i; char board[100][100], response; clrscr(); printf(" @@ This is n-queen problem. Enter the number of queens(say n) and watch how computer places them in (n x n) matrix such that none can meet another moving along horizontally, vertically or digonally. "); printf(" Enter the number of queens : "); scanf("%d", &no_of_queens); if(no_of_queens > 23) { printf(" @@ Thought the program is OK for any queen value.But due the configuration of the output screen the output will be tranketed (A very large queen number may cause the system stack overflow). So it is highly recommended that you run the program with maximum queen number 23..."); printf(" Want to continue(Y/N)?"); fflush(stdin); scanf("%c", &response); if(toupper(response) == 'N') return (0); } else if(no_of_queens < 3) { printf("The number of Queen must be greater than 3."); getch(); return (0); } printf("Want a row number below the board(Y/N) : "); fflush(stdin); response_row = (char)getchar(); if(toupper(response_row) == 'Y') flagrow = 1; printf("Want a column number below the board(Y/N) : "); fflush(stdin); response_col = (char)getchar(); if(toupper(response_col) == 'Y') flagcol = 1; clrscr(); printf("M/c in work ! Please Wait..."); // This for-loop is used for checking all the columns of row 0 only... _setcursortype(_NOCURSOR); for(col = 0; col < no_of_queens; col++) { memset(board, '-', sizeof(board)); check( 0, col, board ); } clrscr(); printf("Thank you for seeing this program through."); getch(); return (0); } void check( int r, int c, char board[100][100] ) { int i, j; // Terminating condition for the recursion... if ( ( r == no_of_queens ) && ( c == 0 )) { clrscr(); printf(" (%d-Queen) Set : %d ", no_of_queens, count++); print( board ); fflush(stdin); ch = (char)getch(); clrscr(); if(ch == 'e') exit (0); printf("M/c in work ! Please Wait..."); } // Vertical check... for(i = 0; i < r; i++) { if ( board[i][c] == queen) return; } // Horizontal check... for(j = 0; j < c; j++) { if ( board[r][j] == queen) return; } // Left-Diagonal check... i = r; j = c; do { if ( board[i][j] == queen ) return; i--; j--; } while( i >= 0 && j >= 0 ); // Right-Diagonal check... i = r; j = c; do { if ( board[i][j] == queen ) return; i--; j++; } while( i >= 0 && j < no_of_queens ); // Placing the queen if the ckecked position is OK... board[r][c] = queen; r++; // This for-loop is used for checking all the columns for each row //starting from 1 upto the end... for(int p = 0; p < no_of_queens; p++) check(r, p, board); for(int h = 0; h < no_of_queens; h++) board[r - 1][h] = '-'; } void print(char board[100][100]) { for(int i = 0; i < no_of_queens; i++) { if(flagrow == 1) printf("%3d", i + 1); for(int j = 0; j < no_of_queens; j++) { if(board[i][j] == queen) { textcolor(RED); cprintf("%3c", queen); } else { textcolor(8); //dark gray cprintf("%3c", 22); } } printf(" "); } textcolor(7); if(flagcol == 1) { if(flagrow) printf(" "); for(i = 0; i < no_of_queens; i++) printf("%3d", i + 1); } gotoxy(62, 1); printf("Press E to exit."); textcolor(7); }

Related Source Codes

 Script Name Author The Game Opposite as seen on Nokia 2300 Mobile Manikanta RECURSIVE BALANCED QUICK SORT ashish Radix Sort ashish Change your mouse pointer Ashim The blinking star Shashank Data Validation Crylittlebaby To search a file by giving file type like mp3 or mpeg or doc Prashanth SR Menus Demonstration B.Chidhambaram Employee Database Project Using C. Reenku Raman Nayak Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ Calendar Program Omkar & Devendra Stop double Process for start in C Cedrik Jurak Stop double Process for start in C Cedrik Jurak Time Scheduler Atiq Anwar A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T

Subscribe to SourceCodesWorld - Techies Talk
Email:

 Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home