Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

Can anyone check my code in placing queens in a chessboard game?

OpenStudy (anonymous):

@electrokid

OpenStudy (anonymous):

what do you mean placing queens? how?

OpenStudy (anonymous):

can you look at my code? i'll post it

OpenStudy (anonymous):

which language are you using?

OpenStudy (anonymous):

c++

OpenStudy (koikkara):

well, Check mate....

OpenStudy (anonymous):

its been 11hrs, I slept, woke up and no code yet.

OpenStudy (anonymous):

bool ChessBoard::PlaceQueens (int c) { int num=n, rows=0; if (0<=c && c<n) { while (rows<n) { if (Safe (rows,c)==true) { SetQueen (rows, c); if (PlaceQueens(c+1)) return true; else { RemoveQueen (rows, c); rows++; } } else if (Safe(rows,c)==false && rows==(n-1)) { rows--; c--; RemoveQueen (rows, c); } row++; } return false; } else return true; } ======================================= int* queen; bool* row;

OpenStudy (anonymous):

what is the condition for placing the queens? any restriction .. any order?

OpenStudy (anonymous):

Any order as long as it's always safe

OpenStudy (anonymous):

and you have a member function for "Safe(int, int)"

OpenStudy (anonymous):

yes, but i'm not sure if it's right =============================================== bool ChessBoard::Safe (int r, int c) const { int i=1; while (0<=r-i && 0<=c-i) { if (queen[c-1]==0 && row[r-1]==false) if (queen[c-1]==0 && row[r]==false) if (queen[c]==0 && row[r-1]==false) return true; i++; } i=1; while (r+i<n && c+i<n) { if (queen[c+i]==0 && row[r+i]==false) if (queen[c+i]==0 && row[r]==false) if (queen[c]==0 && row [r+1]==false) return true; i++; } return false; }

OpenStudy (mew55):

hey is it okay if u can look at my program in my question?

OpenStudy (anonymous):

this one sounds correct. the easy way to check would be to use a "putchar('*')" at a grid location that is safe. and check by logic

OpenStudy (anonymous):

to begin with, try with a 5x5 grid

OpenStudy (anonymous):

I tried 4, this is how it looks

OpenStudy (anonymous):

um pretty

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!