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;
}
}
}
}
Related
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(f,"1 1\n%d %d",m,n);
return 0;
}
which generated this test_case:
21 50
0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 1 1 0 0 1 0 1
0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 1 1 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 0 1 0 0 0 0
1 1 1 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 1 0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 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1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 1 1
0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0
0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 1
1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 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 1 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;
}
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.
I have this two-dimensial array array[y][x] (where x is horizontal and y vertical):
3 2 0 0 0 0 0 0 0 0
1 4 3 0 0 0 0 0 0 0
2 4 0 0 0 0 0 0 0 0
2 4 0 0 0 0 0 0 0 0
1 3 0 0 0 0 0 0 0 0
4 2 5 1 0 0 0 0 0 0
1 3 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0
And I need to print it like this:
3 1 2 2 1 4 1 2 2
2 4 4 4 3 2 3 3 3
3 5
1
How would I do this using c++?
Note that there are no empty lines. If the whole column only has zero's in them, there shouldn't be an endl
You need to iterate over and print out each element. You can flip the elements around by swapping the indices used to get the value out of the array.
#include<iostream>
#include<iomanip>
int gridWidth = 10;
int gridHeight = 10;
int cellWidth = 2;
for (int i = 0; i < gridHeight; i++){
bool anyVals = false;
for (int j = 0; j < gridWidth; j++){
int val = array[i][j]; //Swap i and j to change the orientation of the output
if(val == 0){
std::cout << std::setw(cellWidth) << " ";
}
else{
anyVals = true;
std::cout << std::setw(cellWidth) << val;
}
}
if(anyVals)
std::cout << std::endl;
}
Remember that if you swap i and j then you will need to swap gridWidth and gridHeight.
Just to avoid confusion std::setw(cellWidth) thing is a convenient way to print fixed-width text (like text that must always be two characters long). It takes whatever you print out and adds spaces to it to make it the right length.
Take the transpose of your matrix , make 2 loops(outer and inner ) and print only if the no is greater than zero and print space for every zero. when you go back again to the outer loop ,print new line .
Something like this should help you.
for (int i = 0; i < y; i++)
for (int j = 0; j < x; j++)
if (array[i][j] != 0)
cout << array[i][j];
else
cout << " ";
cout << endl;
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
I am learning a bit of programming and I am trying to code an Ant Colony algorithm for QAP, the problem is that sometimes I get segmentation fault and when I use valgrind it tells me "Address 0x0 is not stack'd, malloc'd or (recently) free'd". This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream> //ifstream
#include <string>
#include <sstream>
#include <vector>
#include <utility> //pair
#include <iostream>
#include <algorithm> //shuffle
#include <random> //default_random_engine
#include <chrono> //chrono::system_clock
#include <cstdlib> // rand
#include <unistd.h>
#include <csignal>
using namespace std;
sig_atomic_t volatile done = 0;
void game_over(int) { done = 1; }
int funCosto(int dim,vector<int> sol,vector<int> dist, vector<int> flujo){
int costo = 0;
for (int i = 0; i<dim;i++){
for (int j = i+1; j<dim;j++){
// El costo es la * del valor de los flujos por la permutacion de las distancias
costo += flujo[dim*i+j] * dist[dim*(sol[i]-1) + (sol[j]-1)];
}
}
return costo*2; //La matriz es simétrica
}
int buscarLista(vector<int> lista, int elemento, int dim){
int aux = 0;
for (int i = 0; i < dim; i++){
if (lista[i] == elemento){aux = 1;}
}
return aux;
}
vector<int> localSearch(int dim,vector<int> sol, vector<int> dist, vector<int> flujo){
vector<int> solActual(dim), solAux(dim);
solActual = sol;
int mejorCosto = funCosto(dim,solActual,dist,flujo);
int costoActual, mejorCostoAnterior;
do{
mejorCostoAnterior = mejorCosto; // para determinar que se llegó a un óptimo local
for (int i = 0; i < dim; i++){
for (int j = i+1; j < dim; j++){
solAux = solActual;
solAux[i] = solActual[j];
solAux[j] = solActual[i]; //intercambiamos dos elementos
/*Importante, hay que optimizar el cálculo del costo de los vecinos*/
costoActual = funCosto(dim,solAux,dist,flujo);
if (costoActual<mejorCosto){
break;
}
}
if (costoActual < mejorCosto){
mejorCosto = costoActual; //se actualiza el mejor costo
solActual = solAux; //se efectua el movimiento
break;
}
}
} while (mejorCosto < mejorCostoAnterior); //se detiene cuando ya no hay mejoría
return solActual;
}
pair<int,vector<int>> antColony(int numAnts, int dim, vector<int> dist, vector<int> flujo){
done = 0;
std::signal(SIGALRM, game_over);
alarm(300);
vector<vector<int>> hormigas(numAnts, vector<int>(dim));
vector<vector<int>> hormigasNuevas(numAnts, vector<int>(dim));
vector<vector<double>> feromonas(dim, vector<double>(dim));
vector<int> mejorSol, mejorSolNueva;
double mejorCosto, mejorCostoNuevo, totalFer, prob, auxProb, probAcum = 0 ;
int numSwaps = dim/3; // número de cambios que va a hacer cada hormiga en cada iteración
random_device rd; // obtener un número aleatorio de hardware
mt19937 eng(rd()); // semilla para el generador
uniform_int_distribution<> disInt(0,dim-1);
uniform_real_distribution<> disReal(0,1);
int prim, seg, aux, contadorRep, sinMejoria = 0; //indices
int inten = 1; //la intensificación está prendida al comienzo
for (int i = 1; i <= dim; i++){
hormigas[0][i-1] = i;
}
for (int j = 1; j < numAnts; j++){
hormigas[j] = hormigas[0];
}
//obtener una semilla basada en el tiempo
for (int k = 0; k < numAnts; k++){
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
shuffle(hormigas[k].begin(),hormigas[k].end(), default_random_engine(seed));
}
for (int i = 0; i < numAnts; i++){ //aplica local Search para las soluciones aleatorias
hormigas[i] = localSearch(dim,hormigas[i],dist,flujo);
}
mejorCosto = funCosto(dim,hormigas[0],dist,flujo);
mejorSol = hormigas[0];
for (int i = 1; i < numAnts; i++){
if (funCosto(dim,hormigas[i],dist,flujo) < mejorCosto){
mejorCosto = funCosto(dim,hormigas[i],dist,flujo);
mejorSol = hormigas[i];
}
}
for (int i = 0; i<dim; i++){
for (int j = 0 ; j<dim; j++){
feromonas[i][j] = 1.0/(100*mejorCosto);
}
}
for (int z = 0; z < 1; z++){ //máximo número de iteraciones
for (int i = 0; i < numAnts; i++ ){
hormigasNuevas[i] = hormigas[i];
for (int j = 0; j < numSwaps; j++){
prim = disInt(eng);
auxProb = disReal(eng);
seg = -1;
probAcum = 0;
do{
seg++;
if (seg == prim){seg++;}
totalFer = 0; //limpiamos para esta iteración
for (int k = 0; k < dim; k++){
if (k != prim){
totalFer += feromonas[prim][hormigasNuevas[i][k]-1] + feromonas[k][hormigasNuevas[i][prim]-1];
}
}
//construimos la probabilidad con la que es aceptado el cambio segun las feromonas
prob = (feromonas[prim][hormigasNuevas[i][seg]-1]+feromonas[seg][hormigasNuevas[i][prim]-1]) / totalFer;
probAcum += prob;
}while ((auxProb > probAcum) && (seg < dim));
if (seg == dim){seg--;}
aux = hormigasNuevas[i][prim];
hormigasNuevas[i][prim] = hormigasNuevas[i][seg];
hormigasNuevas[i][seg] = aux;
}
//mejoramos la solución modificada por la hormiga
hormigasNuevas[i] = localSearch(dim,hormigasNuevas[i],dist,flujo);
}
//contadorRep = 0;
//intensificación
for (int a = 0; a < numAnts; a++){
if (inten == 1){
if (funCosto(dim,hormigasNuevas[a],dist,flujo) < funCosto(dim,hormigas[a],dist,flujo)){
hormigas[a] = hormigasNuevas[a];
}
else{ contadorRep++;} //para contar cuantas veces se queda con la respuesta anterior
}
else{
hormigas[a] = hormigasNuevas[a];
}
}
//if (contadorRep == numAnts){inten = 0;} //apaga la intensificación
mejorCostoNuevo = funCosto(dim,hormigas[0],dist,flujo);
for (int b = 1; b < numAnts; b++){
if (funCosto(dim,hormigas[b],dist,flujo) < mejorCostoNuevo){
mejorCostoNuevo = funCosto(dim,hormigas[b],dist,flujo);
mejorSolNueva = hormigas[b];
}
}
if (mejorCostoNuevo < mejorCosto){
//si se consigue una mejor solucíón
mejorCosto = mejorCostoNuevo;
mejorSol = mejorSolNueva;
inten = 1;
sinMejoria = 0;
}
//actualiación de la matriz de feromonas
for (int c = 0; c < dim; c++){
for (int d = 0; d < dim; d++){ //Evaporación
feromonas[c][d] = 0.9*feromonas[c][d];
}
}
for (int e = 0; e < dim; e++){//reforzamos la mejor solución
feromonas[e][mejorSol[e]-1] = feromonas[e][mejorSol[e]-1] + 0.1/mejorCosto;
}
//diversificación
if (sinMejoria > dim/2){
hormigas[0] = mejorSol; //se queda la mejor solución hasta el momento
//se vuelve a inicializar las hormigas
for (int k = 1; k < numAnts; k++){
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
shuffle(hormigas[k].begin(),hormigas[k].end(), default_random_engine(seed));
}
for (int i = 0; i < numAnts; i++){ //aplica local Search para las soluciones aleatorias
hormigas[i] = localSearch(dim,hormigas[i],dist,flujo);
}
mejorCosto = funCosto(dim,hormigas[0],dist,flujo);
mejorSol = hormigas[0];
for (int i = 1; i < numAnts; i++){
if (funCosto(dim,hormigas[i],dist,flujo) < mejorCosto){
mejorCosto = funCosto(dim,hormigas[i],dist,flujo);
mejorSol = hormigas[i];
}
}
//renovar la matriz de feromonas
for (int i = 0; i<dim; i++){
for (int j = 0 ; j<dim; j++){
feromonas[i][j] = 1.0/(100*mejorCosto);
}
}
}
sinMejoria++;
}
pair <int,vector<int>> pairSol = make_pair (mejorCosto,mejorSol);
return pairSol;
}
int main (int argc, char* argv[]) {
vector<int> resultados(10);
for (int i = 0; i < 10; i++){
clock_t startTime = clock();
ifstream file(argv[1]);
int dim; //dimensiones de las matrices
file >> dim;
vector<int> suc(dim*dim); //matriz con los flujos entre las sucursales
vector<int> loc(dim*dim); //matriz con las distancias de las localidades
pair <int,vector<int>> pairSol; //tiene el costo de la busqueda y la permutación
//guardar la matriz de distancia
for (int i = 0; i < dim; i++){
for (int j = 0; j < dim; j++) {
file >> suc[dim*i+j];
}
}
//guardar la matriz de flujos
for (int i = 0; i < dim; i++){
for (int j = 0; j < dim; j++) {
file >> loc[dim*i+j];
}
}
pairSol = antColony(10,dim,loc,suc);
resultados[i] = pairSol.first;
cout << pairSol.first << endl;
for (int i = 0; i < dim; i++){
cout << pairSol.second[i] << " ";
}
cout << endl;
cout << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
}
int total = 0;
for (int j = 0; j<10; j++){
total += resultados[j];
}
cout << endl << "El promedio de de las soluciones es: " <<endl;
cout << total/10 << endl;
return 0;
}
Valgrind gives me this:
Invalid read of size 4 at 0x804B331: main (antColony.cpp:269)
Address 0x0 is not stack'd, malloc'd or (recently) free'd
Process terminating with default action of signal 11 (SIGSEGV)
Access not within mapped region at address 0x0
at 0x804B331: main (antColony.cpp:269)
I am using this file:
20
0 87 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
87 0 0 82 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
14 0 0 0 0 68 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 82 0 0 0 0 19 18 38 47 0 0 0 0 0 0 0 0 0 0
0 57 0 0 0 0 0 0 0 0 91 0 0 0 0 0 0 0 0 0
0 0 68 0 0 0 0 0 0 0 0 77 0 0 0 0 0 0 0 0
0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0 0 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9 3 8 0 4 4 2 5 6 4 9
9 4 8 1 9 1 4 9 6 3 8 2 4 0 3 2 8 7 2 8
8 4 7 2 8 9 6 4 5 3 2 7 4 3 0 4 3 3 8 4
2 2 7 2 6 5 4 1 3 6 4 6 2 2 4 0 8 5 9 9
9 7 4 4 1 3 7 8 4 4 8 4 5 8 3 8 0 1 2 7
3 6 3 2 9 3 4 4 9 1 1 5 6 7 3 5 1 0 1 1
4 3 7 7 2 7 9 9 8 8 9 4 4 2 8 9 2 1 0 4
3 5 1 8 6 3 6 1 1 6 6 1 9 8 4 9 7 1 4 0
The error comes from this lines near the end:
for (int i = 0; i < dim; i++){
cout << pairSol.second[i] << " ";
}
Where I am trying to print the solution vector.
I don't understand the problem with the memory this is generating. This is more confusing because sometimes it works perfectly. If someone could explain it to me I would be grateful.
Thanks in anticipation for your help!
I have debugged your program a bit, and found that when the error happens, the pairSol.second is actually empty.
The only way I see this could happen is if mejorSolNueva is never assigned a value, but then assignment mejorSol = mejorSolNueva happens.
It looks like problem in this part of the code:
mejorCostoNuevo = funCosto(dim,hormigas[0],dist,flujo);
for (int b = 1; b < numAnts; b++){
if (funCosto(dim,hormigas[b],dist,flujo) < mejorCostoNuevo){
mejorCostoNuevo = funCosto(dim,hormigas[b],dist,flujo);
mejorSolNueva = hormigas[b];
}
}
if (mejorCostoNuevo < mejorCosto){
//si se consigue una mejor solucíón
mejorCosto = mejorCostoNuevo;
mejorSol = mejorSolNueva;
inten = 1;
sinMejoria = 0;
}
In the loop you have condition
if (funCosto(dim,hormigas[b],dist,flujo) < mejorCostoNuevo){
And next after it you have
if (mejorCostoNuevo < mejorCosto){
mejorSol gets assigned when mejorCostoNuevo is greater than result of funCosto. But then you are checking if mejorCostoNuevo is less than mejorCosto.
Shouldn't these two conditions be in correspondednce?
Changing second condition to
if (mejorCostoNuevo > mejorCosto){
stopped producing errors for me. Anyway if condition in for loop is not true and it mejorCostoNuevo is less than mejorCosto, then you have mejorSol of zero size and thus the error.