Add to Favorites    Make Home Page 6654 Online  
 Language Categories  
 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 placed
in (n x n) matrix in such a way that none will meet any other wile moving
horizontally, vertically or along diagonal with respect to its position.
The program is able to take any input, but for better output try it with
not more than 23.
Category C » Beginners / Lab Assignments
Hits 367374
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 <iostream.h> #include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> 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




Google Groups 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.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=645


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review