Why is this BFS queue size so large? - c++

I am recently working with BFS algorithm. I made a maze solving program with this algorithm. here is my code:
#include<iostream>
#include<cmath>
#include<sstream>
#include<vector>
#include<queue>
#include <unistd.h>
using namespace std;
int m,n;
inline int indexFor(int x,int y) {
return (x-1) + m*(y-1);
}
int main() {
FILE *file = fopen("test_case","r"); // test_case file
//build maze
fscanf(file,"%d",&m);
fscanf(file,"%d",&n);
vector<int> grid((m+1)*(n+1));
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
int k;
fscanf(file,"%d",&k);
grid[indexFor(i,j)] = k;
}
}
int startX,startY,endX,endY;
fscanf(file,"%d",&startX);
fscanf(file,"%d",&startY);
fscanf(file,"%d",&endX);
fscanf(file,"%d",&endY);
//bfs starts
queue<int> x,y;
x.push(1);
y.push(1);
vector<int> visited((m+1)*(n+1),0);
vector<vector<int>> path_track((m+1)*(n+1),vector<int>(2,0));
while(x.size()>0) {
int k = x.front(), l = y.front();
x.pop();
y.pop();
visited[indexFor(k,l)]=1;
// this part for printing each steps
cout << "\033[2J\033[H";
cout << "queue size: " << x.size()<<endl;
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
if(visited[indexFor(i,j)]==1) cout << "* ";
else {
if(grid[indexFor(i,j)]==0) {
cout << ". ";
} else {
cout << "# ";
}
}
}
cout << endl;
}
usleep(10);
if(k-1>0) {
if(grid[indexFor(k-1,l)] != 1 && visited[indexFor(k-1,l)]!=1) {
x.push(k-1);
y.push(l);
path_track[indexFor(x.back(),y.back())] = {k,l};
}}
if((k+1)<=m) {
if(grid[indexFor(k+1,l)] != 1 && visited[indexFor(k+1,l)]!=1) {
x.push(k+1);
y.push(l);
//cout << "Path_track: "<<x.back() << ", " << y.back() << " : " << k << ", " << l <<endl;
path_track[indexFor(x.back(),y.back())] = {k,l};
}}
if(l-1>0) {
if(grid[indexFor(k,l-1)] != 1 && visited[indexFor(k,l-1)]!=1) {
x.push(k);
y.push(l-1);
path_track[indexFor(x.back(),y.back())] = {k,l};
}}
if(l+1<=n) {
if(grid[indexFor(k,l+1)] != 1 && visited[indexFor(k,l+1)]!=1) {
x.push(k);
y.push(l+1);
path_track[indexFor(x.back(),y.back())] = {k,l};
}}
}
cout << "\033[2J\033[H";
vector<vector<int>> path;
path.push_back({endX,endY});
while(endX!=0&&endY!=0) {
int kx = endX, ky = endY;
endX = path_track[indexFor(kx,ky)][0];
endY = path_track[indexFor(kx,ky)][1];
path.push_back({endX,endY});
}
path.pop_back();
if(path[path.size()-1][0] == startX && path[path.size()-1][1] == startY)
{
for(vector<int> l:path) grid[indexFor(l[0],l[1])] = 8;
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
if(grid[indexFor(i,j)]==8) {
cout << "X ";
} else {
if(grid[indexFor(i,j)]==0) {
cout << ". ";
} else {
cout << "# ";
}
}
}
cout << endl;
}
} else {
cout << "No path found\n";
for(vector<int> l:path) grid[indexFor(l[0],l[1])] = 8;
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
if(grid[indexFor(i,j)]==8) {
cout << "X ";
} else {
if(grid[indexFor(i,j)]==0) {
cout << ". ";
} else {
cout << "# ";
}
}
}
cout << endl;
}
}
//cout<<path_track[indexFor(2,2)][0]<<","<<path_track[indexFor(2,2)][1]<<endl;
return 0;
}
I made a maze_generator for this problem which is :
#include <iostream>
#include <vector>
using namespace std;
int m,n;
inline int indexFor(int x,int y) {
return m * (x-1) + (y-1);
}
int main() {
srand(time(nullptr));
FILE * f = fopen("test_case","w");
cin >> m;
cin >> n;
vector<int> grid(m*n,0);
fprintf(f,"%d %d\n",m,n);
for(int i = 0; i<grid.size();i++) {
int k = (rand()%7);
if(i == 0 || i==grid.size()-1 ) k = 0;
grid[i] = k<5?0:1;
}
grid[indexFor(m,n)] = 0;
for(int i = 1;i<=m;i++) {
for(int j = 1; j<=n;j++) {
cout << grid[indexFor(i,j)] << " ";
fprintf(f,"%d ",grid[indexFor(i,j)]);
}
cout<<endl;
fprintf(f,"\n");
}
fprintf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 0 0 1 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 0 1 1 1 0 0
1 1
21 50
In the test case the last two line represents starting and ending point for the maze. In case for maze 0 indicates free path and 1 indicates wall. 1st line denotes the size of maze.
Problem is that when I solve it the queue size jumped to 130k+ for this particular test_case. Is there something I missing? or it is normal?

I solved the problem. Looks like i needed use another array for queue mapping to check if a point is already queued. Here is the final solution:
#include<iostream>
#include<cmath>
#include<sstream>
#include<vector>
#include<queue>
#include <unistd.h>
using namespace std;
int m,n;
inline int indexFor(int x,int y) {
return (x-1) + m*(y-1);
}
int main() {
FILE *file = fopen("test_case","r");
fscanf(file,"%d",&m);
fscanf(file,"%d",&n);
vector<int> grid((m+1)*(n+1));
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
int k;
fscanf(file,"%d",&k);
grid[indexFor(i,j)] = k;
}
}
int startX,startY,endX,endY;
fscanf(file,"%d",&startX);
fscanf(file,"%d",&startY);
fscanf(file,"%d",&endX);
fscanf(file,"%d",&endY);
queue<int> x,y;
x.push(1);
y.push(1);
vector<int> visited((m+1)*(n+1),0);
vector<int> queued((m+1)*(n+1),0); // <-- THIS IS WHAT I ADDED
vector<vector<int>> path_track((m+1)*(n+1),vector<int>(2,0));
while(x.size()>0) {
int k = x.front(), l = y.front();
x.pop();
y.pop();
visited[indexFor(k,l)]=1;
cout << "\033[2J\033[H";
cout << "queue size: " << x.size()<<endl;
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
if(visited[indexFor(i,j)]==1) cout << "* ";
else {
if(grid[indexFor(i,j)]==0) {
cout << ". ";
} else {
cout << "# ";
}
}
}
cout << endl;
}
usleep(10000);
if(k-1>0) {
if(grid[indexFor(k-1,l)] != 1 && visited[indexFor(k-1,l)]!=1&&!queued[indexFor(k-1,l)]) {
x.push(k-1);
y.push(l);
queued[indexFor(x.back(),y.back())] = 1;
path_track[indexFor(x.back(),y.back())] = {k,l};
}}
if((k+1)<=m) {
if(grid[indexFor(k+1,l)] != 1 && visited[indexFor(k+1,l)]!=1&&!queued[indexFor(k+1,l)]) {
x.push(k+1);
y.push(l);
//cout << "Path_track: "<<x.back() << ", " << y.back() << " : " << k << ", " << l <<endl;
path_track[indexFor(x.back(),y.back())] = {k,l};
queued[indexFor(x.back(),y.back())] = 1;
}}
if(l-1>0) {
if(grid[indexFor(k,l-1)] != 1 && visited[indexFor(k,l-1)]!=1&&!queued[indexFor(k,l-1)]) {
x.push(k);
y.push(l-1);
path_track[indexFor(x.back(),y.back())] = {k,l};
queued[indexFor(x.back(),y.back())] = 1;
}}
if(l+1<=n) {
if(grid[indexFor(k,l+1)] != 1 && visited[indexFor(k,l+1)]!=1&&!queued[indexFor(k,l+1)]) {
x.push(k);
y.push(l+1);
queued[indexFor(x.back(),y.back())] = 1;
path_track[indexFor(x.back(),y.back())] = {k,l};
}}
}
cout << "\033[2J\033[H";
vector<vector<int>> path;
/*
for(int i=1;i<=m;i++) {
for(int j =1;j<=m;j++) {
cout << path_track[indexFor(i,j)][0] << "," << path_track[indexFor(i,j)][1]<< " ";
}
cout << endl;
}
*/
path.push_back({endX,endY});
while(endX!=0&&endY!=0) {
int kx = endX, ky = endY;
endX = path_track[indexFor(kx,ky)][0];
endY = path_track[indexFor(kx,ky)][1];
path.push_back({endX,endY});
}
path.pop_back();
if(path[path.size()-1][0] == startX && path[path.size()-1][1] == startY)
{
for(vector<int> l:path) grid[indexFor(l[0],l[1])] = 8;
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
if(grid[indexFor(i,j)]==8) {
cout << "X ";
} else {
if(grid[indexFor(i,j)]==0) {
cout << ". ";
} else {
cout << "# ";
}
}
}
cout << endl;
}
} else {
cout << "No path found\n";
for(vector<int> l:path) grid[indexFor(l[0],l[1])] = 8;
for(int i=1;i<=m;i++) {
for(int j =1;j<=n;j++) {
if(grid[indexFor(i,j)]==8) {
cout << "X ";
} else {
if(grid[indexFor(i,j)]==0) {
cout << ". ";
} else {
cout << "# ";
}
}
}
cout << endl;
}
}
//cout<<path_track[indexFor(2,2)][0]<<","<<path_track[indexFor(2,2)][1]<<endl;
return 0;
}

Related

Loading a 2D array from an input file into a Function

I am having trouble loading a 10x10 array from an input file and storing it into an array. I have written this so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void LoadImage(const string imagefile, int image[MAXROWS][MAXCOLS]) //Function to load in image
{
ifstream inputs;
int i,j;
inputs.open(imagefile.c_str());
getline(inputs, imagefile[i][j]);
inputs.ignore(10000,'\n');
if (inputs.is_open())
{
for( i=0; i < MAXROWS; i++ )
{
for ( j=0; i < MAXCOLS; j++ )
{
inputs >> image[i][j];
}
}
}
inputs.close();
}
The void LoadImage function and was given to me with those specific parameters to use or the main function will not execute.
An example of an input file:
#Sample Image--1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Where I have to get rid of the header of the input file before building the array.
If I compile what I have now I get the "error: invalid types ‘const char[int]’ for array subscript
getline(inputs, imagefile[i][j]);"
I understand why I am getting the error, but I do not know how to fix it.
I appreciate any help I can get!
The code is generally OK, just following the comments and fixing a few small mistakes should work.
Note specially the terminating condition for the following loop:
for ( j=0; i < MAXCOLS; j++ )
Should be instead:
for ( j=0; j < MAXCOLS; j++ )
This is the reason why you're getting an infinite loop.
Here's the complete code:
#include <iostream>
#include <fstream>
#include <string>
#define MAXROWS 10
#define MAXCOLS 10
using namespace std;
void LoadImage(const string imagefile, int image[MAXROWS][MAXCOLS]) //Function to load in image
{
ifstream inputs;
int i,j;
inputs.open(imagefile.c_str());
if (inputs.is_open())
{
for( i=0; i < MAXROWS; i++)
{
for ( j=0; j < MAXCOLS; j ++)
{
std::string str;
inputs >> image[i][j];
}
}
}
inputs.close();
}
void PrintImage(int image[MAXROWS][MAXCOLS])
{
int i,j;
for(i = 0; i < MAXROWS; i++)
{
for (j = 0; j < MAXCOLS; j ++)
{
cout << image[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int image[MAXROWS][MAXCOLS] = {0,};
LoadImage ("img.mtx", image);
PrintImage (image);
}
And testing:
$ cat img.mtx
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
$ g++ main.cpp && ./a.out
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
The line
getline(inputs, imagefile[i][j]);
does not make sense.
If you want to ignore the first line of the input file, then you should simply use inputs.ignore, as you are already doing afterwards. So you can simply delete the line which calls getline.
Another problem is that the line
for ( j=0; i < MAXCOLS; j++ )
should probably be
for ( j=0; j < MAXCOLS; j++ )
i.e. you wrote i instead of j.

Initialize 2d Array at compile and allow user input

I am trying to declare an array that is sized at runtime/compile through an overload constructor.
private:
auto** arr = new int[n][n];
overloadConstruct(int n){
arr[n][n] = {0,0};
}
This does not work, it says the second n needs to be constant and auto is not allowed. Any help would be appreciated. I am not sure of all the rules with arrays, especially 2d arrays when doing this. I just need to be able to size a 2d array at runtime/compile through an input.
Use a vector of vectors instead:
#include <iostream>
#include <vector>
class Foo
{
public:
Foo( const int rowCount, const int colCount )
: m_2dArray( rowCount, std::vector<int>( colCount ) ) // initialize the vector
{ // member like this
}
void printArr() const
{
for ( size_t row = 0; row < m_2dArray.size( ); ++row )
{
for ( size_t col = 0; col < m_2dArray[ 0 ].size( ); ++col )
{
std::cout << m_2dArray[ row ][ col ] << " ";
}
std::cout << '\n';
}
}
private:
std::vector< std::vector<int> > m_2dArray; // the vector member
};
int main ()
{
int rowCount { };
int colCount { };
std::cout << "Enter row count: ";
std::cin >> rowCount;
std::cout << "Enter column count: ";
std::cin >> colCount;
std::cout << "\nThe contents of 2D array:\n";
Foo obj( rowCount, colCount ); // construct the object using user's input
obj.printArr( );
}
The output:
Enter row count: 10
Enter column count: 20
The contents of 2D array:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
As you can see, the user can enter the values at runtime.

Values in array are changing when exiting

So I'm trying to implement breadth first search for determining the shortest path in a bipartite graph for a project. The problem I am having is that the values of my initial arrays are changing from the "bfs" function to the "printShortestPath" function as can be seen at the end. What would be causing the array values to randomly change?
Header:
#define BGRAPH_H
using namespace std;
#include <vector>
#include <list>
class bgraph {
public:
bgraph(int numV);
void addEdge(int v1, int v2);
bool bfs(int v1, int v2);
void printShortestPath(int v1, int v2);
void removeDuplicates();
private:
int numVertex;
vector<vector<int>> adjacencyLists;
bool visited[];
int pred[];
int dist[];
};
#endif
Cpp
using namespace std;
#include "bgraph.h"
#include <list>
#include <set>
#include <iostream> // TEST
bgraph::bgraph(int numV) {
numVertex = numV;
adjacencyLists.resize(numV);
bool visited[numV] = {false};
int pred[numV];
int dist[numV];
cout << "got to constructor\n";
for (int i = 0; i < numVertex; i++) {
pred[i] = -1;
dist[i] = 1000;
}
cout << "pred:\n";
for (int i = 0; i < numVertex; i++)
cout << pred[i] << " ";
cout << endl << endl;
cout << "dist:\n";
for (int i = 0; i < numVertex; i++)
cout << dist[i] << " ";
cout << endl << endl;
cout << "visited:\n";
for (int i = 0; i < numVertex; i++)
cout << visited[i] << " ";
cout << endl << endl;
}
void bgraph::addEdge(int v1, int v2) {
adjacencyLists.at(v1).push_back(v2);
adjacencyLists.at(v2).push_back(v1);
}
bool bgraph::bfs(int v1, int v2) {
list<int> queue;
visited[v1] = true;
dist[v1] = 0;
queue.push_back(v1);
// Look at values of pred, dist, and visisted
cout << "pred:\n";
for (int i = 0; i < numVertex; i++)
cout << pred[i] << " ";
cout << endl << endl;
cout << "dist:\n";
for (int i = 0; i < numVertex; i++)
cout << dist[i] << " ";
cout << endl << endl;
cout << "visited:\n";
for (int i = 0; i < numVertex; i++)
cout << visited[i] << " ";
cout << endl << endl;
while (!queue.empty()) {
int currentVertex = queue.front();
queue.pop_front();
for (int i = 0; i < adjacencyLists.at(currentVertex).size(); i++) {
int nextVertex = adjacencyLists.at(currentVertex).at(i);
if (!visited[nextVertex]) {
//cout << "Next vertex: " << nextVertex << endl; // TEST
visited[nextVertex] = true;
dist[nextVertex] = dist[currentVertex] + 1;
//cout << "dist[nextVertex]: " << dist[nextVertex] << endl; // TEST
pred[nextVertex] = currentVertex;
//cout << "pred[nextVertex]: " << pred[nextVertex] << endl; // TEST
queue.push_back(nextVertex);
if (nextVertex == v2) {
return true;
}
}
}
}
return false;
}
void bgraph::printShortestPath(int v1, int v2) {
if (!bfs(v1, v2)) {
cout << "No connection.\n";
return;
}
vector<int> shortestPath;
int iter = v2;
shortestPath.push_back(iter);
while (pred[iter] != -1) {
shortestPath.push_back(pred[iter]);
iter = pred[iter];
}
cout << "Path is: \n";
for (int i = shortestPath.size() - 1; i >= 0; i--)
cout << shortestPath.at(i) << " ";
cout << endl;
}
void bgraph::removeDuplicates() {
set<int> noDuplicates;
for (int i = 0; i < adjacencyLists.size(); i++) {
for (int j = 0; j < adjacencyLists.at(i).size(); j++)
noDuplicates.insert(adjacencyLists.at(i).at(j));
adjacencyLists.at(i).clear();
if (noDuplicates.find(i) != noDuplicates.end())
noDuplicates.erase(i);
set<int>::iterator iter = noDuplicates.begin();
while (iter != noDuplicates.end()) {
adjacencyLists.at(i).push_back(*iter);
iter++;
}
noDuplicates.clear();
}
}
Output in constructor:
pred:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
dist:
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
visited:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Output of same arrays when they're being called in bfs function:
pred:
12124608 0 0 0 12131016 32566 -1417405256 32764 -1417405245 32764 -1417405245 32764 0 0 0 0 0 0 12158624 32566 8 0 -1417405256 32764 11 0 1634493778 824206708 3487008 0 12131304 32566 6 0 0 0 4098 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 -1417405176 32764 12158624 32566 0 0 0 0 -1417405344 32764 12157120 32566 12157008 32566 12157024 32566 0 0 0 1
dist:
12124608 0 0 0 12131016 32566 -1417405256 32764 -1417405245 32764 -1417405245 32764 0 0 0 0 0 0 12158624 32566 8 0 -1417405256 32764 11 0 1634493778 824206708 3487008 0 12131304 32566 6 0 0 0 4098 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 -1417405176 32764 12158624 32566 0 0 0 0 -1417405344 32764 12157120 32566 12157008 32566 12157024 32566 0 0 0 1
visited:
192 1 185 0 0 0 0 0 0 0 0 0 0 0 0 0 200 26 185 0 54 127 0 0 184 28 132 171 252 127 0 0 195 28 132 171 252 127 0 0 195 28 132 171 252 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 160 134 185 0 54 127 0 0 8 0 0 0 0 0 0 0 184 28 132 171 252 127 0 0 11 0 0 0
Any help is really appreciated.

Checking if matrix of 1s and 0s are connected - C++

The goal is to create a matrix where all the 0s are connected (touching either up, down, left or right). I generated a 10/19 matrix of 0s and randomly added 1s. Now I want to step through the matrix and change values such that all 0s are connected, but I am struggling with that part. Any help is greatly appreciated!
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main () {
//generate matrix of 0s
int map[10][19] = {{0}};
int a = 10;
int b = 19;
//display matrix
for (int i = 0; i < a; ++i) {
for (int j = 0; j < b; ++j) {
std::cout << map[i][j] << ' ';
}
std::cout << std::endl;
}
cout << "\n" << endl;
//adds 1s to matrix
for (int i = 0; i < a; ++i) {
for (int j = 0; j < b; ++j) {
map[i][j] = 0 + rand() % 2;
}
}
//display matrix
for (int i = 0; i < a; ++i) {
for (int j = 0; j < b; ++j) {
std::cout << map[i][j] << ' ';
}
std::cout << std::endl;
}
cout << "\n" << endl;
First output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Second output:
1 1 1 0 0 0 0 0 1 1 0 1 0 0 1 1 1 1 0
0 1 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 1
1 1 1 0 1 0 1 1 0 1 0 1 1 1 0 0 0 0 0
0 0 1 0 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0
1 0 1 1 1 0 1 1 1 1 1 0 1 0 0 1 1 0 0
0 0 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 1 1
1 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0
0 1 1 1 0 1 1 0 1 0 1 0 0 0 1 1 0 0 0
1 1 0 1 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 1 1 1 1 1 0 1 1 1 0 1 1 0

Trouble understanding recursion in this application

I am writing a recursive solver for a Sudoku puzzle. I store blank places with 0's so that my program can read them more easily. I have the original puzzle stored starting from subscript one in order to better visualize the grid. I am not sure I have a full grasp on the recursion and that is where the problem is. I am getting output that seems like it is on track to solve the puzzle, but it is leaving zeros there that shouldn't be there. I think it has something to do with the placement of my unsetSquare, or with the return statements.
Here is a sample of the output...
**************************************************
7 4 3 | 8 2 1 | 5 6 8
2 6 8 | 0 9 0 | 0 1 0
0 0 0 | 0 0 6 | 0 0 4
---------|---------|---------
0 0 0 | 0 0 0 | 2 3 9
0 0 0 | 0 0 0 | 0 0 0
4 1 5 | 0 0 0 | 0 0 0
---------|---------|---------
9 0 0 | 5 0 0 | 0 0 0
0 2 0 | 0 1 0 | 7 4 0
0 0 0 | 2 0 0 | 9 0 5
**************************************************
**************************************************
7 4 3 | 8 2 1 | 5 6 9
2 6 8 | 0 9 0 | 0 1 0
0 0 0 | 0 0 6 | 0 0 4
---------|---------|---------
0 0 0 | 0 0 0 | 2 3 9
0 0 0 | 0 0 0 | 0 0 0
4 1 5 | 0 0 0 | 0 0 0
---------|---------|---------
9 0 0 | 5 0 0 | 0 0 0
0 2 0 | 0 1 0 | 7 4 0
0 0 0 | 2 0 0 | 9 0 5
**************************************************
**************************************************
7 4 3 | 8 2 1 | 5 6 0
2 6 8 | 1 9 0 | 0 1 0
0 0 0 | 0 0 6 | 0 0 4
---------|---------|---------
0 0 0 | 0 0 0 | 2 3 9
0 0 0 | 0 0 0 | 0 0 0
4 1 5 | 0 0 0 | 0 0 0
---------|---------|---------
9 0 0 | 5 0 0 | 0 0 0
0 2 0 | 0 1 0 | 7 4 0
0 0 0 | 2 0 0 | 9 0 5
**************************************************
**************************************************
7 4 3 | 8 2 1 | 5 6 0
2 6 8 | 2 9 0 | 0 1 0
0 0 0 | 0 0 6 | 0 0 4
---------|---------|---------
0 0 0 | 0 0 0 | 2 3 9
0 0 0 | 0 0 0 | 0 0 0
4 1 5 | 0 0 0 | 0 0 0
---------|---------|---------
9 0 0 | 5 0 0 | 0 0 0
0 2 0 | 0 1 0 | 7 4 0
0 0 0 | 2 0 0 | 9 0 5
**************************************************
**************************************************
7 4 3 | 8 2 1 | 5 6 0
2 6 8 | 3 9 0 | 0 1 0
0 0 0 | 0 0 6 | 0 0 4
---------|---------|---------
0 0 0 | 0 0 0 | 2 3 9
0 0 0 | 0 0 0 | 0 0 0
4 1 5 | 0 0 0 | 0 0 0
---------|---------|---------
9 0 0 | 5 0 0 | 0 0 0
0 2 0 | 0 1 0 | 7 4 0
0 0 0 | 2 0 0 | 9 0 5
**************************************************
notice at the end of the first row, it goes to 8 looking for a solution, then to 9, 9 is not legal and it has reached the end of the for loop, so it replaces it with a zero and it continues. How can I make it go back to try different numbers in the first row to get a more complete solution?
Here is my recursion function...
bool DoTheWork::addSquare(int& depth, ostream& outStream){
for(int i = ONE; i <= NINE; ++i){
for(int j = ONE; j <= NINE; ++j){
if(i == NINE && j == NINE && board.getSquare(NINE, NINE) != ZERO){
cout << i << " " << j << endl;
return true;
}
//cout << "original" << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
if(board.getSquare(i, j) == ZERO){
//cout << "original: " << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
for(int k = ONE; k <= NINE; ++k){
board.setSquare(i, j, k);
board.display(outStream);
if(board.isLegal()){
return addSquare(depth, outStream);
}
else{
board.unsetSquare(i, j);
}
}
}
}
}
board.display(outStream);
return false;
}
I can see a problem:
It should be:
if(board.isLegal()){
if( addSquare(depth, outStream))
return true;
}
Means if the whole board is solved then roll back.
EDIT think about it:
you put 1 in the first square, it returns false. don't you want to try put 2?
EDIT2
More problem:
if(i == NINE && j == NINE && board.getSquare(NINE, NINE) != ZERO){
cout << i << " " << j << endl;
return true;
}
Is this suppose to be a completion check? you check only 1 square. and in your sample it is filled!
you need to check that there is no 0.
EDIT3:
bool DoTheWork::addSquare(int& depth, ostream& outStream){
//use flag to let you know if all completed
bool zeroFound = false;
for(int i = ONE; i <= NINE; ++i){
for(int j = ONE; j <= NINE; ++j){
//cout << "original" << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
if(board.getSquare(i, j) == ZERO){
zeroFound = true;
//cout << "original: " << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
for(int k = ONE; k <= NINE; ++k){
board.setSquare(i, j, k);
board.display(outStream);
if(board.isLegal()){
if(addSquare(depth, outStream)){
return true;
}
else{
board.unsetSquare(i, j);
}
}
}
}
}
}
board.display(outStream);
return !zeroFound; //true in case it is full!
}
It turns out I had somethings in the wrong order, and not in the right scope. After successfully doing it with a while loop, I found the solution with a for loop.
bool DoTheWork::addSquare(int& depth, ostream& outStream){
for(int i = 1; i < 10; ++i){
for(int j = 1; j < 10; ++j){
if(board.getSquare(i, j) == 0){
if(i == 10){
return true;
}
for(int k = 1; k <= 9; ++k){
board.setSquare(i, j, k);
if(board.isLegal() && addSquare(depth, outStream)){
return true;
}
}
board.unsetSquare(i, j);
return false;
}
}
}
}