Add to Favorites    Make Home Page 2200 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Algorithms Home » Graphic simulation of the n_Queen problem using Backtracking.

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

Search Projects & Source Codes:

Title Graphic simulation of the n_Queen problem using Backtracking.
Author Abhishek H Dwivedi
Author Email yarrows [at] indiatimes.com
Description This C code will solve the n-Queen problem using the
backtracking approach.The object of this problem is to place n Queens in
an nXn chess-board, such that none of them can threaten each othe in one
move.It graphically shows the movement of queens, as the algorithm runs.It
halts
momentarily whenever a solution is found, for the user to view it. It
resumes to find other solutions on hitting a key at this juncture.When the
queen of the 1st row reaches the last column, and since there is no row
behind it to backtrack to, the program exits.The user can exit the program
by hitting any key,during the normal running of the program.

Parameters :None.The user will be asked to enter the size of the
chess-board(eg. Enter 8 if you want to solve for an 8X8 chess-board).
Category C++ » Algorithms
Hits 332676
Code Select and Copy the Code
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<dos.h> int diag45[12],sol[12],n,chess[12][12],r1,c1,num=0,i,x1,x2,y2,y1; int num1,num2,m=0,x3; void main() { int gd=DETECT,gm,x,y; void queens(int,int); clrscr(); printf("Enter the size of the chess board (12 is the limit) "); scanf("%d",&n); initgraph(&gd,&gm,"c:\tc\bgi"); x=getmaxx(); y=getmaxy(); setcolor(WHITE); x1=y1=0; x2=x/n; y2=y/n; if(x2>=10) { if(x2>y2) x2=y2; else y2=x2; for(i=0;i<=n;i++) { delay(250); line(x1,y1,x1,y); if(i==1) num1=x1/2; x1=x1+x2; //y1=y1+y2; //x2=x2+x/n; //y2=y2+y/n; } x1=x1-x2; x3=x1; for(i=0;i<=n;i++) { delay(250); line(x1,y1,0,y1); //x1=x1+x2; if(i==1) num2=y1/2; y1=y1+y2; //x2=x2+x/n; //y2=y2+y/n; } x1=(x/n)/2; y1=(y/n)/2; } /* if(x2>10)*/ else { x2=y2=10; for(i=0;i<=n;i++) { delay(250); line(x1,y1,x2,y2); x1=x1+10; } for(i=0;i<=n;i++) { delay(250); line(x1,y1,0,y1); y1=y1+10; } x1=5; y1=5; } delay(250); for(i=0;i<n;i++) sol[i]=32767; for(i=0;i<n;i++) {for(int j=0;j<n;j++) {chess[i][j]=j; }} for(int j=0;j<n;j++) diag45[j]=sol[j]=32767; //sol[1]=1; //int c1=0; //while(c1!=7) //{ num1=x1; queens(0,c1); //c1++; //} setcolor(WHITE); outtextxy(x3,y1,"All solutions are over "); printf(" Total number of solutions is %d ",num); getch(); } void queens(int r,int c) { delay(10); int d45,d135,atck=0; if(r>(n-1)||c>(n-1)) printf("Nonsensical condition reached "); if(m==1) { setcolor(BLACK); setfillstyle(1,BLACK); fillellipse(x1+((c-1)*x2),y1+(r*y2),7,7); } m=0; setcolor(WHITE); setfillstyle(1,WHITE); fillellipse(x1+((c)*x2),y1+(r*y2),6,6); delay(350); //printf("r is %d ; c is %d ",r,c); //getch(); /*delay(100); printf("r is %d ; c is %d ",r,c);*/ d45=r-c; for(i=0;i<r;i++) { r1=abs(r-i); c1=abs(c-sol[i]); if((chess[r][c]!=sol[i])&&(d45!=diag45[i])&&(r1!=c1))//This condition continue; //checks for an else //attack { atck=1; break; }} //If the queen is safe :- if(atck!=1) { //setcolor(WHITE); //pieslice(x1+(c*num2*2),y1+(r*num1*2),0,360,4); sol[r]=c; diag45[r]=r-c; if(r==(n-1)) { //printf(" A possible solution is : "); //for(i=0;i<n;i++) //printf("%d ",sol[i]); //printf(" "); num++; getch(); if(c==(n-1)) { //sol[r-1]=sol[r-1]+1; setcolor(BLACK); setfillstyle(1,BLACK); fillellipse(x1+((n-1)*x2),y1+((n-1)*y2),7,7); m=1; //queens(r-1,sol[r-1]); } else { m=1; queens(r,c+1); }} //*********// else { /*for(i=0;i<r;i++) printf("%d ",sol[i]);*/ sol[r+1]=0; queens(r+1,0); if(c!=(n-1)) queens(r,c+1); else { setcolor(BLACK); setfillstyle(1,BLACK); fillellipse(x1+((c)*x2),y1+(r*y2),6,6); printf(""); }}} //If an attack is taking place :- else { setfillstyle(1,BLACK); setcolor(BLACK); fillellipse(x1+(c*x2),y1+(r*y2),6,6); if(r!=0) { if(c!=(n-1)) queens(r,c+1); else { m=1; setbkcolor(RED); delay(100); setbkcolor(BLACK); printf(""); }} else { if(c!=(n-1)) printf(""); // else // printf("All solutions are over ! "); } }} ForwardSourceID:NT0000740A ----- Forwarded by Amit M/BLR/TCS on 09/30/2004 09:52 AM ----- <newcode@sourcecodesworld.com> 09/28/2004 06:00 PM Please respond to <rajamukesh@gmail.com> To <amit@vyomworld.com> cc <amit.m@tcs.com> Subject *** SourceCode: New Code Added *** (Add this code in the Language with following Id) Language Id:1 ScriptName: program to implement who command Content Type:1 Type :37 New Category: Author :raja mukesh Author Email:rajamukesh@gmail.com Description:program to implementing who command using system calls Parameters : Code : #include<stdio.h> #include<sys/utsname.h> #include<utmp.h> int main(void) { struct utmp *n; char *a; int i; setutent(); n=getutent(); while(n!=NULL) { if(n->ut_type==7) { printf("%-9s",n->ut_user); printf("%-12s",n->ut_line); a=ctime(&n->ut_time); printf(" "); for(i=4;i<16;i++) printf("%c",a[i]); printf(" ("); printf("%s",n->ut_host); printf(") "); } n=getutent(); } } ForwardSourceID:NT0000741E ----- Forwarded by Amit M/BLR/TCS on 09/30/2004 09:52 AM ----- <newcode@sourcecodesworld.com> 09/29/2004 05:41 AM Please respond to <vivek_patel9@rediffmail.com> To <amit@vyomworld.com> cc <amit.m@tcs.com> Subject *** SourceCode: New Code Added *** (Add this code in the Language with following Id) Language Id:2 ScriptName: Inventory Management System Content Type:1 Type :3 New Category: Author :Vivek Patel Author Email:vivek_patel9@rediffmail.com Description:This software is designed to manage your inventory system, with a facility of adding, modifying, deleting, viewing, searching, and analyzing a item stored in an inventory. This all functionality is very well comprised using Graphics Outlet for user to go on with it!!! Parameters : Code : /**************************************************************/ /****************INVENTORY MANAGEMENT SYSTEM*******************/ /**************************************************************/ /**************************************************************/ /*Programmed by : Vivek Patel**********************************/ /*For Bugs Free feel to contact********************************/ /*Website : www.vivekpatel.cjb.net*****************************/ /*Email : vivek_patel9@rediffmail.com**************************/ /**************************************************************/ #include<iostream.h> #include<conio.h> #include<iomanip.h> #include<fstream.h> #include<graphics.h> #include<dos.h> #include<string.h> #include<stdio.h> #include <time.h> fstream inoutfile; //Menu Global Item #define pixTOrc(x) (8*(x-1)) //convert pixel into row and col format #define INC 5 //Increment Distance Between Menu Items #define ROW 15 //Row Value for Menu Item #define COL 8 //Column Value for Menu Item // To display the Inventory Main menu options typedef char option[15]; option mainMenu[]= { "New Record", "Display", "Search", "Updation", "Deletion", "Analysis", "Exit" }; /*-------------------Inventory Class--------------------*/ class Inventory{ char itemNo[2],itemName[20]; int qty; double price,amt; public: char *getno(){return itemNo;} char *getitem(){ return itemName;} double getamt(){return amt;} void getdata(); void showdata(int,int); void showspecific(); void alterspecific(char *,char *); }; void Inventory :: getdata(){ gotoxy(30,12); cout<<"Enter Item Number : ?"; cin>>itemNo; gotoxy(30,14); cout<<"Enter Item Name : ?"; cin>>itemName; gotoxy(30,16); cout<<"Enter Quantity : ?"; cin>>qty; gotoxy(30,18); cout<<"Enter Price : ?"; cin>>price; amt = price * qty; } void Inventory :: showdata(int x,int y){ gotoxy(x,y); cout.setf(ios::left,ios::adjustfield); cout<<setw(3)<<itemNo; cout.setf(ios::left,ios::adjustfield); cout<<setw(13)<<itemName; cout<<setw(4)<<qty; cout.setf(ios::right,ios::adjustfield); cout.setf(ios::showpoint); cout.setf(ios::fixed,ios::floatfield); cout<<setprecision(2)<<setw(8)<<price; cout.setf(ios::right,ios::adjustfield); cout.setf(ios::showpoint); cout.setf(ios::fixed,ios::floatfield); cout<<setprecision(2)<<setw(15)<<amt; } void Inventory :: showspecific(){ gotoxy(30,13); cout<<"--Search Item Found--"; gotoxy(30,15); cout<<"Item No : "; cout.setf(ios::left,ios::adjustfield); cout<<itemNo; gotoxy(30,17); cout<<"Item Name : "; cout.setf(ios::left,ios::adjustfield); cout<<itemName; gotoxy(30,19); cout<<"Quantity : "; cout<<qty; cout.setf(ios::right,ios::adjustfield); cout.setf(ios::showpoint); cout.setf(ios::fixed,ios::floatfield); gotoxy(30,21); cout<<"Price : "; cout<<setprecision(2)<<price; gotoxy(30,23); cout<<"Amount : "; cout.setf(ios::right,ios::adjustfield); cout.setf(ios::showpoint); cout.setf(ios::fixed,ios::floatfield); cout<<setprecision(2)<<amt; } void Inventory :: alterspecific(char itmno[2],char itmname[20]){ strcpy(itemNo,itmno); strcpy(itemName,itmname); gotoxy(30,16); cout<<"Enter Quantity : ?"; cin>>qty; gotoxy(30,18); cout<<"Enter Price : ?"; cin>>price; amt = price * qty; } /*---------------Inventory Codes End------------------*/ /*--------------Menu and all other functions Code--------------*/ //Displays Graphic text in delaying fashion void displayMe(int x,int y,const char *ch,int delayTime){ char d[2]; int len=strlen(ch); for(int i=0;i<=len;i++) { d[0]=ch[i]; d[1]='

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

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-2022 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=605


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