You have the same problem. You need to allocate N elements before you can index them.
1 2 3 4
|
vector<vector<int> > nQueens(int BoardSize)
{
vector<vector<int> > solutions;
vector<int> column_solutions( BoardSize );
|
When I run the code it produces:
Total number of solutions for board size 4X4 are 1
1 (1,1) (2,1) (3,1) (4,1) |
That's a list of (row,column), so what it is saying is that your solution is:
+---+---+---+---+
| Q | | | |
+---+---+---+---+
| Q | | | |
+---+---+---+---+
| Q | | | |
+---+---+---+---+
| Q | | | |
+---+---+---+---+
Which is, obviously, not correct. (A 4x4 board has exactly two solutions, and they are a mirror image.
You need to think through the way the algorithm works. We'll start just by thinking of the first row. We already know that each row has exactly one queen in it. So, let's try a queen in
every position in the first row and see if it works.
Pass 1 Pass 2 Pass 3 Pass 4
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+ +---+---+---+---+
| Q | | | | | | Q | | | | | | Q | | | | | | Q |
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+ +---+---+---+---+
So far, so good -- you can put a queen in every spot. Now, for each possible position of the queen there, you need to try the same thing with the rows
below the first.
This is where we can generalize -- where the recurrence comes in. For the row below, we'll try to put a queen in every position, just like we did in the first. The only difference is that we'll automatically reject positions where there cannot be a queen.
Again, for each
valid position for a queen in the second row, we'll try all the positions for the
third row. And so on until we do all the rows.
So, what are the ending conditions?
If we find a valid position for a queen in the current row, try all the positions for the next row.
If there is no next row (we are on the last row), then we've found a complete solution. Append it to the list of solutions.
But what happens if we don't find a valid position in the
current row?
We need to back up. Go back the the previous row and try the next solution.
Q--- Q--- Q--- Q--- Q--- Q--- Q--- Q---
Q--- -Q-- --Q- --Q- --Q- --Q- --Q- --Q-
---- ---- ---- Q--- -Q-- --Q- ---Q ----Q hmm, no valid solution.
---- ---- ---- ---- ---- ---- ---- ----
Q---
---Q here, on the previous row, we'll try the next queen position. See?
----
----
(For here on out, I'll avoid drawing
every possible position for the queens, and only draw the
valid positions for the rows above the one we're commenting on.)
Q---
---Q
-Q--
----Q argh, all the way at the end (row 3), all out of positions
Q---
---Q
----Q out of positions on row 2
----
Q---
----Q out of positions on row 1[tt]
----
----
-Q-- next position on row 0
---Q good on row 1
Q--- good on row 2
--Q- good on row 3. Hey, row 3 is the last row! This is a valid solution!
Having found a valid solution, we
solutions.push_back(v1D);
.
But we're not done! Keep going!
-Q--
---Q
Q---
----Q no good on row 3
-Q--
---Q
----Q no good on row 2
----
-Q--
----Q no good on row 1
----
----
--Q-
Q---
---Q
-Q-- hey, another good one on row 3 --> another valid solution. But, we're not done! Nope. Keep going.
--Q-
Q---
---Q
----Q no good on row 3
--Q-
Q---
----Q no good on row 2
----
--Q-
----Q no good on row 1
----
----
---Q
<snip>
----Q no good on row 0. Since we have exhausted all possibilities on row 0, we're done.
----
----
----
I've got to go. Hope this helps.
[edit] Edited a little for readability.