Queens problem

Well... this is the N queens problem... I'm using 5 queens because it is small and I have couts showing me each step so I can see where I'm going wrong. I thought I was checking thoroughly, but the queens just keep stacking in the left most column. I'm assuming a bool would fix that... but I just need help on where to put it and what to put in it? I thought by "drawing" out the paths and the queens, I could avoid them stacking... Sorry it's such a long code, but I sincerely appreciate any help! The extra couts and displayBoard functions are a really helpful visual tool to see what's going wrong.


compiled in Visual C++ 2008 Express
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
using namespace std; 

void displayBoard(int board[][5]);
void placeQueen(int board [][5]);
int findColumn(int row, int board[][5]);
void findHorizontalPath(int row, int column, int board[][5]);
void findVerticalPath(int row, int column, int board[][5]);
void findNEPath(int row, int column, int board[][5]);
void findSEPath(int row, int column, int board[][5]);
void findSWPath(int row, int column, int board[][5]);
void findNWPath(int row, int column, int board[][5]);
void findEmpty(int board[][5]);


int main()
{
	int board[5][5];
	
	findEmpty(board);
	cout << "Find empty" << endl;
	displayBoard(board);

	placeQueen(board);
	cout << "Place Queen" << endl;
	displayBoard(board);




}
void displayBoard(int board[][5])
{
	char show = ' ';
	for(int i = 0; i < 5; i++)
	{
		cout << endl;
		for(int j = 0; j < 5; j++)
		{
			switch(board[i][j])
			{
			case 0:
				show = 'e';
				break;
			case 1:
				show = 'Q';
				break;
			case 2:
				show = 'X';
				break;
			}

			cout << " " << show << " ";
		}
	}
}

void placeQueen(int board[][5])
{
	int  row = 0,column = 0;
	for(int i = 0; i < 5; i++)
	{
		row = i;
		column = findColumn(row, board);
		board[row][column] = 1;
		cout << "horizontal" << endl;
		findHorizontalPath(row,column,board);
		displayBoard(board);

		cout << "vertical" << endl;
		findVerticalPath(row,column,board);
		displayBoard(board);

		cout << "ne" << endl;
		findNEPath(row, column, board);
		displayBoard(board);

		cout << "se" << endl;
		findSEPath(row, column, board);
		displayBoard(board);

		cout << "sw" << endl;
		findSWPath(row, column, board);
		displayBoard(board);

		cout << "nw" << endl;
		findNWPath(row, column, board);
		displayBoard(board);
	}


}

int findColumn(int row, int board[][5])
{
	int temp = -1;
	for(int i = 0; i < 5; i++)
	{
		board[row][i];
		if(temp != 1 && temp != 2)
			return i;
	}

}
void findHorizontalPath(int row, int column, int board[][5])
{
	for(int i = 0; i < 5; i++)
	{
		if(board[row][i] != 1)
			board[row][i] = 2;

	}
}
void findVerticalPath(int row, int column, int board[][5])
{
	for(int i = 0; i < 5; i++)
	{
		if(board [i][column] != 1)
			board [i][column] = 2;
	}
}
void findNEPath(int row, int column, int board[][5])
{ 
	for(int j=row-1, k = column+1 ;j >= 0; --j, k++)  
    {
        if(board[j][k] != 1)
            board[j][k] = 2;
    }
}
void findSEPath(int row, int column, int board[][5])
{
     for(int j=row+1, k = column+1 ;j < 5; j++, k++)  
    {
        if(board[j][k] != 1)
            board[j][k] = 2;
    }
}
void findSWPath(int row, int column, int board[][5])
{
     for(int j=row+1, k = column-1 ;k >= 0; j++, --k)  
    {
        if(board[j][k] != 1)
            board[j][k] = 2;
    }
}
void findNWPath(int row, int column, int board[][5])
{
    for(int h= row-1, i = column-1 ;h >= 0; --h, --i)  
    {
        if(board[h][i] != 1)
			board[h][i] = 2;
    }
 
   
}

void findEmpty(int board[][5])
{
	int checkMatrix;
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			checkMatrix = board[i][j];
			if(checkMatrix != 1 || checkMatrix != 2)
				board[i][j] = 0;
		}
	}
}


Last edited on
Bump. I'm pretty sure my main problem lies in the "int findColumn" function.
I added this function, but my queens are still stacking in the first column... :(

called in findColumn:
1
2
3
4
5
6
7
8
9
int findColumn(int row, int board[][5])
{
	for(int i = 0; i < 5; i++)
	{
		if(isSafe(row, i, board))
			return i;
	}

}


new function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool isSafe(int row, int column, int board[][5])
{
	int checkRow;
	int checkColumn;
	int checkDiagonal;
	//check if vertical and horizontal is safe
	for(int i = 0; i < 8; i++)
	{
		checkRow = board[row][i];
		checkColumn = board[i][column];
		if(checkRow != 0 || checkColumn != 0)
			return false;
	}
	//check if NE is safe
	for(int j=row-1, k = column+1 ;j >= 0; --j, k++)  
    {
		checkDiagonal = board[j][k];
		if(checkDiagonal != 0)
			return false;
    }
	//check if SE is safe
	for(int a=row+1, b = column+1 ;a < 5; a++, b++)  
    {
		checkDiagonal = board[a][b];
        if(checkDiagonal != 0)
            return false;
    }
	//check if SW is safe
	for(int c=row+1, d = column-1 ;d >= 0; c++, --d)  
    {
		checkDiagonal = board[c][d];
        if(checkDiagonal != 0)
			return false;
    }
	//check if NW is safe
	for(int e= row-1, f = column-1 ;e >= 0; --e, --f)  
    {
		checkDiagonal = board[e][f];
        if(checkDiagonal != 0)
			return false;
    }

	return true;


}
Last edited on
Topic archived. No new replies allowed.