Click here to Skip to main content
15,912,457 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: parsing C++ header file Pin
David Crow9-Nov-07 4:26
David Crow9-Nov-07 4:26 
AnswerRe: parsing C++ header file Pin
Hamid_RT9-Nov-07 5:18
Hamid_RT9-Nov-07 5:18 
AnswerRe: parsing C++ header file Pin
Michael Dunn8-Nov-07 21:45
sitebuilderMichael Dunn8-Nov-07 21:45 
GeneralRe: parsing C++ header file Pin
DevMentor.org8-Nov-07 22:05
DevMentor.org8-Nov-07 22:05 
GeneralRe: parsing C++ header file Pin
toxcct9-Nov-07 3:13
toxcct9-Nov-07 3:13 
AnswerRe: parsing C++ header file Pin
Bram van Kampen10-Nov-07 14:43
Bram van Kampen10-Nov-07 14:43 
GeneralRe: parsing C++ header file Pin
DevMentor.org10-Nov-07 15:00
DevMentor.org10-Nov-07 15:00 
Question5Queens/5Knights on 8x8 board Pin
Ramper8-Nov-07 15:12
Ramper8-Nov-07 15:12 
I am currently trying to create a program to find all possibilities for placing 5 queens and 5 knights on an 8x8 board in such a way nothing will attack anything else. There are 16 possible solutions. My problem i am encountering, is when placing queens. It is not generating all possibilities. I believe it is just taking the first instance it finds and going with that rather than examining the rest of the row for possible solutions. Here is what I have, if you could possibly help it would be greatly appreciated.




#include <iostream>
#include <string>

using namespace std;
class QueenKnight
{

public:
QueenKnight()
{

queens = 0;
knight = 0;
row = 8;
column = 8;
count = 0;
for(int i= 0; i<row; i++)
="" {
="" for(int="" j="0;" j<column;="" j++)
="" board[i][j]="." ;
="" }
=""
="" void="" updateboardq(int="" r,="" int="" c)
="" i="0;" i<row;="" if(i="=" r)
="" if(j="" !="c)
" if(="" (i-j)="=" (r-c)="" )
="" j!="c)
" (i+j)="=" (r+c)="" placequeen(int="" startr,="" startc)
="" if(board[startr][startc]="=" ".")
="" board[startr][startc]="Q" updateboardq(startr,="" startc);
="" if(board[i][j]="" if(queens="" <="" 5)
="" updateboardq(i,j);
="" queens++;
="" }

="" countfree()
="" 8;="" j<8;="" count++;
="" return="" count;
=""

="" displayboard()
="" i<="" row;="" cout="" <<board[i][j]="" <<"="" ";
="" <<endl;
="" bool="" checkknight(int="" i,int="" j)="" returns="" false="" if="" position="" is="" not="" acceptable
="" if((i+2)="" &&="" (j-1)="">=0)
{
if(board[(i+2)][(j-1)] == "Q" || board[(i+2)][(j-1)] == "K")
{
return false;
}
}
if((i+2) <= 7 && (j+1) <=7)
{
if(board[(i+2)][(j+1)] == "Q" || board[(i+2)][(j+1)] == "K")
{
return false;
}
}
if((i-2) >= 0 && (j-1) >=0)
{
if(board[(i-2)][(j-1)] == "Q" || board[(i-2)][(j-1)] == "K")
{
return false;
}
}
if((i-2) >= 0 && (j+1) <= 7)
{
if(board[(i-2)][(j+1)] == "Q" || board[(i-2)][(j+1)] == "K")
{
return false;
}
}
if((i+1) <= 7 && (j-2) >=0)
{
if(board[(i+1)][(j-2)] == "Q" || board[(i+1)][(j-2)] == "K")
{
return false;
}
}
if((i+1) <= 7 && (j+2) <=7)
{
if(board[(i+1)][(j+2)] == "Q" || board[(i+1)][(j+2)] == "K")
{
return false;
}
}
if((i-1) >= 0 && (j-2) >=0)
{
if(board[(i-1)][(j-2)] == "Q" || board[(i-1)][(j-2)] == "K")
{
return false;
}
}
if((i-1) >= 0 && (j+2) <= 7)
{
if(board[(i-1)][(j+2)] == "Q" || board[(i-1)][(j+2)] == "K")
{
return false;
}
}

return true;
}


void placeKnight()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
if(board[i][j] == ".")
{

if(checkKnight(i,j))
{
board[i][j] = "K";
knight++;
}
}
}
}
}
int knightCount()
{
return knight;
}




private:

int row;
int queens;
int column;
string board[8][8];
int count;
int knight;
};

int main()
{
int displays = 0;

for(int k=0; k<64; k++)
{

QueenKnight example;
example.placeQueen((k/8), (k % 8));
if(example.countFree() > 4)
{
example.placeKnight();

//cout <<"display: "<<displays<< endl;
="" if(example.knightcount()=""> 2)
// {
example.displayBoard();
cout <
AnswerRe: 5Queens/5Knights on 8x8 board Pin
Luc Pattyn8-Nov-07 17:47
sitebuilderLuc Pattyn8-Nov-07 17:47 
AnswerRe: 5Queens/5Knights on 8x8 board Pin
David Crow9-Nov-07 4:32
David Crow9-Nov-07 4:32 
QuestionMatrix Multiplication Pin
7.3DIESEL8-Nov-07 13:57
7.3DIESEL8-Nov-07 13:57 
AnswerRe: Matrix Multiplication Pin
Nelek8-Nov-07 21:47
protectorNelek8-Nov-07 21:47 
QuestionRDTSC Pin
hxhl958-Nov-07 11:37
hxhl958-Nov-07 11:37 
AnswerRe: RDTSC Pin
Mark Salsbery8-Nov-07 12:37
Mark Salsbery8-Nov-07 12:37 
Questionvariable solution compiles, but I can't get at value: [modified] Pin
e40s8-Nov-07 11:01
e40s8-Nov-07 11:01 
AnswerRe: variable solution compiles, but I can't get at value: Pin
Llasus8-Nov-07 12:53
Llasus8-Nov-07 12:53 
GeneralRe: variable solution compiles, but I can't get at value: Pin
e40s9-Nov-07 4:30
e40s9-Nov-07 4:30 
AnswerOfftopic Pin
Nelek8-Nov-07 21:39
protectorNelek8-Nov-07 21:39 
GeneralRe: Offtopic Pin
e40s9-Nov-07 5:14
e40s9-Nov-07 5:14 
QuestionRe: variable solution compiles, but I can't get at value: Pin
David Crow9-Nov-07 4:39
David Crow9-Nov-07 4:39 
AnswerRe: variable solution compiles, but I can't get at value: [modified] Pin
e40s9-Nov-07 5:02
e40s9-Nov-07 5:02 
GeneralRe: variable solution compiles, but I can't get at value: Pin
David Crow9-Nov-07 5:34
David Crow9-Nov-07 5:34 
GeneralRe: variable solution compiles, but I can't get at value: [modified] Pin
e40s9-Nov-07 6:03
e40s9-Nov-07 6:03 
GeneralRe: variable solution compiles, but I can't get at value: Pin
David Crow9-Nov-07 6:20
David Crow9-Nov-07 6:20 
GeneralRe: variable solution compiles, but I can't get at value: [modified] Pin
e40s9-Nov-07 6:36
e40s9-Nov-07 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.