My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
n-Queens problem implemented in C
Posted Date:
Total Responses: 0
Posted By: Pratik Pansare Member Level: Gold Points/Cash: 5
|
#include #include char board[48][48],row=0; unsigned n; void main() { char i,j; for(clrscr(),printf("Enter the no. of squares.\n");;printf("Max. capacity is 48 X 48 board.\n")) if(scanf("%d",&n),n<49) // obtaining and validating input break; for(i=0;i for(j=0;j board[i][j]=0; if(queens()) // calling main logic printf("Not all possibilities have been shown.\n"); else printf("That's it.\n"); if(!getch()) getch(); } queens() // recursive function implementing backtracking { char col,i; if(row-n) // if last row hasn't been filled { for(col=0;col if(!board[row][col]) // if current square is not attacked by any queen { for(i=0;i { board[row][i]++,board[i][col]++; // row, column if(row+i>=col&&row+i board[row-col+i][i]++; if(row+col>=i&&row+col board[row+col-i][i]++; } if(board[row++][col]=-1,queens()) // RECURSIVELY CALL for filling next row after marking current square as occupied return 1; // exit recursively to main if ESC is pressed to prevent further calculations for(row--,i=0;i { // do the exact reverse process, i.e., decrementing squares under attack board[row][i]--,board[i][col]--; // row, column if(row+i>=col&&row+i board[row-col+i][i]--; if(row+col>=i&&row+col board[row+col-i][i]--; } board[row][col]=0; // change -1 to 0 as only if it was 0 would it have entered if(!board[row][col]) } return 0; } else // if last row has been filled, success! a solution has been found { if(n<25) // if n is small, print large output with grid lines { for(row=0;row { for(col=0;col printf("+-"); for(printf("+\n"),col=0;col if(board[row][col]+1) printf("| "); else printf("|Q"); } for(i=0;i printf("+-"); printf("+\n"); } else // otherwise print without grid lines for(row=0;row for(col=0;col if(board[row][col]+1) putchar(' '); else putchar('Q'); if(getch()-27) printf("Please wait....\n"); else return 1; return 0; } }
|
Project Feedbacks
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|
|
|
| Post Feedback |
|
|
You must Sign In to post a feedback.
|
|
|
|
|
|