ambigous call on recursive sudoku_backtracker function. - c++

This is my program to solve a sudoku puzzle by using a backtracking algorithm. The program will recursively call itself until it is either solved or if it is unsolvable. The problem is that when I run it the compiler says that the sudoku_backtracker() function call in line 19 of sudoku_solver.cpp is ambiguous. Can someone please explain to me why it says that and how can I fix it. If there are other problems I would also appreciate the help. Thanks alot.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "sudoku_solver.hpp"
int main()
{
std::vector< std::vector<int> > board(9,std::vector<int>(9));
int i =0,j=0;
for(std::string line;std::getline(std::cin,line);)
{
if(i==9)
{
i=0;
break;
}
std::stringstream line_stream(line);
for(std::string token;std::getline(line_stream,token,' ');)
{
if(j==9)
{
j=0;
}
board[i][j] = std::stoi(token);
j++;
}
i++;
}
if(sudoku_backtracker(board)==1)
{
for(int i = 0;i<9;i++)
{
for(int j = 0;j<9;j++)
{
std::cout<<board[i][j];
}
std::cout<<endl;
}
}
return 0;
}
/* This is sudoku_solver.hpp placed here for better organization on stackoverflow*/
#ifndef __SUDOKU_SOLVER_HPP__
#define __SUDOKU_SOLVER_HPP__
#include <vector>
int sudoku_backtracker(std::vector< std::vector<int> > board);
std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board);
bool checks_num_is_valid(std::vector< std::vector<int> > board,int row,int column,int number);
#endif //__SUDOKU_SOLVER_HPP__
/*this is sudoku_solver.cpp but I placed it after the header file so I can put it in block on stackoverflow*/
#include "sudoku_solver.hpp"
#include <iostream>
#include <string>
int sudoku_backtracker(std::vector< std::vector<int> > &board)
{
int test_num = 1;
std::pair<int,int> empty_spot = find_empty_spot(board);
if(empty_spot.first == -1)
{
return 1;
}
while(test_num != 10)
{
if(checks_num_is_valid(board,empty_spot.first,empty_spot.second,test_num))
{
board[empty_spot.first][empty_spot.second] = test_num;
int recursive_sudoku = sudoku_backtracker(board);
if(recursive_sudoku==1)
{
return 1;
}
board[empty_spot.first][empty_spot.second] = 0;
}
test_num++;
}
return 0;
}
std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board,int row,int column)
{
for(int row=0;row<9;row++)
{
for(int column=0;column<9;column++)
{
if(board[row][column] == 0){return std::make_pair(row,column);}
}
}
return std::make_pair(-1,-1);
}
bool checks_num_is_valid(std::vector< std::vector<int> > board, int row,int column, int number)
{
bool num_not_in_column = true;
bool num_not_in_row = true;
bool num_not_in_box = true;
//box_start_row as bsr and box_start_column as bsc
//this is the starting point to check the numbers inside the box and make
//sure the test number is valid
int bsr = 0,bsc = 0;
//checks the numbers in the same column but different rows
for(int i =0;i<9;i++)
{
if(i==row){continue;}
if(board[i][column] == number){num_not_in_column = false;break;}
}
//checks numbers in the same row but different columns
for(int i = 0;i<9;i++)
{
if(i==column){continue;}
if(board[row][i] == number){num_not_in_row = false;break;}
}
//checks wether the numer is int the same box
if(row<=2){bsr =0;}
if(row>=3 && row<=5){bsr = 3;}
if(row>=6 && row<=8){bsr = 6;}
if(column <=2){bsc =0;}
if(column>=3 && column<=5){bsc=3;}
if(column>=6 && column<=8){bsc=6;}
//double for loop to check all the values inside the box
for(bsr;bsr<bsr+3;bsr++)
{
for(bsc;bsc<bsc+3;bsc++)
{
if(bsr==row && bsc==column)
{continue;}
else
{
if(board[bsr][bsc] == number)
{
num_not_in_box = false;
}
}
}
}
bool result = num_not_in_row && num_not_in_column && num_not_in_box;
return result;
}

The declaration :
int sudoku_backtracker(std::vector< std::vector<int> > board);
Does not match the definition :
int sudoku_backtracker(std::vector< std::vector<int> > &board) { ... }
^^^

Related

Error cannot convert argument 1 from 'int[][3] to int(&&)[3][3]'

Can anyone solve this for me? I couldn't find why is it giving error on these specific lines. I guess the syntax is right. I have commented the error lines. It is giving error on open.push_back(p) in DFID function and mylist.push_back(p); in GenerateChildren function Please help me on this. Much Thanks
#include <iostream>
#include <algorithm>
#include <list>
#include <string>
using namespace std;
const int n = 3;
int goal[n][n] = { { 1, 2, 3 },{ 8, 0, 4 },{ 7, 6, 5 } };
static int max_depth = 0;
list <int[3][3]> open;
list <string> closed;
bool DFID(int[3][3]);
list<int[3][3]> generateChildren(int[3][3]);
bool isGoal(int [3][3]);
string convertToString(int[3][3]);
bool inClosed(string);
void main()
{
int puzzle[n][n] = { { 1, 2, 3 }, { 8, 6, 4 }, { 7, 0, 5 } };
DFID(puzzle);
}
bool DFID(int p[3][3])
{
open.push_back(p); // Error on this line
open.pop_front();
list<int[3][3]> mylist = generateChildren(p);
list<int[3][3]>::iterator it;
for (it = mylist.begin(); it != mylist.end(); ++it)
{
if (isGoal(*it))
return true;
else
{
string s =convertToString(*it);
if (inClosed(s))
{
continue;
}
else
{
//
}
}
}
}
list<int[3][3]> generateChildren(int p[3][3])
{
//finding zero element
int a = 0, b = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; i < n; j++)
{
if (p[i][j] == 0)
{
a = i;
b = j;
break;
}
}
}
list <int[3][3]> mylist;
if (p[-a][b] != -1)
{
swap(p[a][b], p[--a][b]);
mylist.push_back(p); //Error on this line
}
if (p[a][--b] != -1)
{
swap(p[a][b], p[a][--b]);
mylist.push_back(p); //Error
}
if (p[++a][b] != 3)
{
swap(p[a][b], p[++a][b]);
mylist.push_back(p); //Error
}
if (p[a][++b] != 3)
{
swap(p[a][b], p[a][++b]);
mylist.push_back(p); //Error
}
return mylist;
}
bool isGoal(int p[3][3])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; i < n; j++)
{
if (p[i][j] != goal[i][j]);
return false;
}
}
return true;
}
string convertToString(int p[3][3])
{
string puzz;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
puzz = puzz + to_string(p[i][j]);
}
}
return puzz;
}
bool inClosed(string s)
{
list<string>::iterator it;
for (it = closed.begin(); it != closed.end(); ++it)
{
if (*it == s);
return true;
}
return false;
}
There are multiple problems with the code as you show it.
One issue is that putting data into a container means it either needs to be move or copied. And arrays can't be neither moved nor copied.
Another issue is that e.g.
bool DFID(int[3][3]);
is equal to
bool DFID(int(*)[3]);
That is, the argument is a pointer and not an array. Pointers and arrays are different.
One possible way to solve your problems (both of them) is to use another standard container, such as std::array:
std::array<std::array<int, n>, n> goal;
std::list<std::array<std::array<int, n>, n>> open;
You can simplify the type with a type-alias:
using matrix_type = std::array<std::array<int, n>, n>;
matrix_type goal;
std::list<matrix_type> open;

This sudoku solver runs for a very long time and I don't know why

I am trying to run this program but it just runs for a very long time when it is supposed to only take a couple seconds. I would appreciate it if I could get help on what is wrong with my code and what i need to fix.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "sudoku_solver.hpp"
int main()
{
std::vector< std::vector<int> > board(9,std::vector<int>(9));
int i =0,j=0;
for(std::string line;std::getline(std::cin,line);)
{
if(i==9)
{
i=0;
break;
}
std::stringstream line_stream(line);
for(std::string token;std::getline(line_stream,token,' ');)
{
if(j==9)
{
j=0;
}
board[i][j] = std::stoi(token);
j++;
}
i++;
}
if(sudoku_backtracker(board)==1)
{
for(int i = 0;i<9;i++)
{
for(int j = 0;j<9;j++)
{
std::cout<<board[i][j];
}
std::cout<<endl;
}
}
return 0;
}
sudoku_solver.hpp:
#ifndef __SUDOKU_SOLVER_HPP__
#define __SUDOKU_SOLVER_HPP__
#include <vector>
int sudoku_backtracker(std::vector< std::vector<int> > &board);
std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board);
bool checks_num_is_valid(std::vector< std::vector<int> > board,int row,int column,int number);
#endif //__SUDOKU_SOLVER_HPP__
sudoku_solver.cpp:
#include "sudoku_solver.hpp"
#include <iostream>
#include <string>
int sudoku_backtracker(std::vector< std::vector<int> > &board)
{
int test_num = 1;
std::pair<int,int> empty_spot = find_empty_spot(board);
if(empty_spot.first == -1)
{
return 1;
}
while(test_num != 10)
{
if(checks_num_is_valid(board,empty_spot.first,empty_spot.second,test_num))
{
board[empty_spot.first][empty_spot.second] = test_num;
int recursive_sudoku = sudoku_backtracker(board);
if(recursive_sudoku==1)
{
return 1;
}
board[empty_spot.first][empty_spot.second] = 0;
}
test_num++;
}
return 0;
}
std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board)
{
for(int row=0;row<9;row++)
{
for(int column=0;column<9;column++)
{
if(board[row][column] == 0){return std::make_pair(row,column);}
}
}
return std::make_pair(-1,-1);
}
bool checks_num_is_valid(std::vector< std::vector<int> > board, int row,int column, int number)
{
bool num_not_in_column = true;
bool num_not_in_row = true;
bool num_not_in_box = true;
//box_start_row as bsr and box_start_column as bsc
//this is the starting point to check the numbers inside the box and make
//sure the test number is valid
int bsr = 0,bsc = 0;
//checks the numbers in the same column but different rows
for(int i =0;i<9;i++)
{
if(i==row){continue;}
if(board[i][column] == number){num_not_in_column = false;break;}
}
//checks numbers in the same row but different columns
for(int i = 0;i<9;i++)
{
if(i==column){continue;}
if(board[row][i] == number){num_not_in_row = false;break;}
}
//checks wether the numer is int the same box
if(row<=2){bsr =0;}
if(row>=3 && row<=5){bsr = 3;}
if(row>=6 && row<=8){bsr = 6;}
if(column <=2){bsc =0;}
if(column>=3 && column<=5){bsc=3;}
if(column>=6 && column<=8){bsc=6;}
//double for loop to check all the values inside the box
for(bsr;bsr<bsr+3;bsr++)
{
for(bsc;bsc<bsc+3;bsc++)
{
if(bsr==row && bsc==column)
{continue;}
else
{
if(board[bsr][bsc] == number)
{
num_not_in_box = false;
}
}
}
}
bool result = num_not_in_row && num_not_in_column && num_not_in_box;
return result;
}
You have two nested infinite loops. bsr<bsr+3 is always true, regardless of bsr value. Except when it overflows, but that's undefined behaviour and the compiler is allowed to optimize that to true.
for(bsr;bsr<bsr+3;bsr++)
{
for(bsc;bsc<bsc+3;bsc++)

Abstract Class Output problems

This main program should ask the user to put in some numbers and store them into a dynamic array. The array should then be outputted its contents in a straight line, no end line commands, with a comma in between. I can't figure out how to start the program.
If you guys can help me find a way to do this, I would be eternally thankful!
Here is ListType.h:
#ifndef LISTTYPE_H_INCLUDED
#define LISTTYPE_H_INCLUDED
#include <iostream>
class ListType {
public:
ListType(size_t=10);
virtual ~ListType();
virtual bool insert(int)=0;
virtual bool erase();
virtual bool erase(int)=0;
virtual bool find(int) const=0;
size_t size() const;
bool empty() const;
bool full() const;
void output(std::ostream& out) const;
friend std::ostream& operator << (std::ostream&, const ListType&);
protected:
int *items;
size_t capacity;
size_t count;
};
#endif // LISTTYPE_H_INCLUDED
here is UListType.h:
#ifndef ULISTTYPE_H_INCLUDED
#define ULISTTYPE_H_INCLUDED
#include <iostream>
class UListType: public ListType {
public:
UListType(size_t=10);
bool insert(int);
bool erase(int);
bool find(int) const;
};
#endif // ULISTTYPE_H_INCLUDED
here is OListType.h:
#ifndef OLISTTYPE_H_INCLUDED
#define OLISTTYPE_H_INCLUDED
#include <iostream>
class OListType: public ListType {
public:
OListType(size_t=10);
bool insert(int);
bool erase(int);
bool find(int) const;
};
#endif // OLISTTYPE_H_INCLUDED
here is ListType.cpp:
#include "ListType.h"
ListType::ListType (size_t a) {
capacity = a;
count = 0;
items = new int [capacity];
}
ListType::~ListType() {
delete [] items;
}
bool ListType::erase() {
count = 0;
return 0;
}
size_t ListType::size() const {
return (count);
}
bool ListType::empty() const {
return (count == 0);
}
bool ListType::full() const {
return (count == capacity);
}
void ListType::output(std::ostream& out) const {
for (int i = 0; i < count; i++) {
if (i > 0) {
out << ", ";
}
out << items[i];
}
}
std::ostream& operator << (std::ostream& out, const ListType& my_list) {
my_list.output(out);
return out;
}
here is UListType.cpp
#include "ListType.h"
#include "UListType.h"
UListType::UListType (size_t c): ListType(c) {}
bool UListType::insert(int item) {
if (full()) {
int *newitems;
capacity *=2;
newitems = new int[capacity];
for (size_t i =0; i < count; ++i){
newitems[i] = items[i];
}
delete [] items;
items = newitems;
}
items[count++] = item;
return true;
}
bool UListType::erase(int item) {
bool result = false;
size_t i=0;
while ( i < count && items [i] != item) {
++i;
}
if (i < count) {
items[i] = items[-- count];
result = true;
}
return result;
}
bool UListType::find(int item) const {
size_t i = 0;
while (i < count && items [i] != item) {
++i;
}
return i;
}
here is OListType.cpp
#include "ListType.h"
#include "OListType.h"
OListType::OListType(size_t c): ListType(c) {}
bool OListType::insert(int item) {
size_t i = count;
if (full()) {
int *newitems;
capacity *=2;
newitems = new int[capacity];
while (i > 0 && items[i-1] > item){
newitems[i] = items[i];
}
delete [] items;
items = newitems;
}
items[count++] = item;
return true;
}
bool OListType::erase(int item) {
bool found=false;
size_t i=0, j= count-1, mid;
while (i <= j && !(found)){
mid = (i + j)/2;
if (item < items [mid])
j = mid - 1;
else if (item > items [mid])
i = mid + 1;
found = items [mid] == item;
}
if (found) {
for (i = mid; i < count - 1; ++i) {
items [i] = items [i +1];
}
--count;
}
return found;
}
bool OListType::find (int item) const {
bool found=false;
size_t i=0, j= count-1, mid;
while (i <= j && !(found)){
mid = (i + j)/2;
if (item < items [mid])
j = mid - 1;
else if (item > items [mid])
i = mid + 1;
found = items [mid] == item;
}
return found;
}
#include "ListType.h"
#include "UListType.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
UListType UL;
cout << "How many numbers do you want to put it?" << endl;
int n;
cin >> n;
cout << "All right, enter " << n << " numbers:" << endl;
int x;
for(int k=0; k<n; ++k)
{
cin >> x;
// do something with x
}
return(0);
}
You already have everything you need. Try the following
#include <iostream>
#include "OListType.h"
using namespace std;
int main()
{
OListType list;
int n;
do
{
cout << "Add a number [Y/n]?";
char a;
cin >> a;
if (a != 'n')
{
cin >> n;
list.insert(n);
}
else
{
list.output(cout);
break;
}
}while (1);
return 0;
}

Spoj Shopping wrong result

can someone give me tricky test example that crash my code. Task --> http://www.spoj.com/problems/SHOP/
I can't find any mistake in my code so I am asking your help.
Code:
http://pastebin.com/6wuFWWJH
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct koord
{
int x;
int y;
koord(int _x=0,int _y=0)
{
x=_x;
y=_y;
}
};
queue<koord>Q;
bool bio[150][150];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int dist[150][150];
char polje[150][150];
int a,b;
void bfs(int a1,int b1)
{
Q.push(koord(a1,b1));
bio[a1][b1]=true;
while(!Q.empty())
{
koord pos=Q.front();
Q.pop();
for(int i=0;i<4;++i)
{
koord dalje=koord(pos.x+dx[i],pos.y+dy[i]);
if(dalje.x>=0 && dalje.x<b && dalje.y>=0 && dalje.y<a)
{
if(polje[dalje.x][dalje.y]!='X' || polje[dalje.x][dalje.y]!='S')
if(bio[dalje.x][dalje.y]==false)
{
if(polje[dalje.x][dalje.y]=='D')
{
bio[dalje.x][dalje.y]=true;
dist[dalje.x][dalje.y]=dist[pos.x][pos.y];
Q.push(dalje);
}
else
{
bio[dalje.x][dalje.y]=true;
dist[dalje.x][dalje.y]=dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0');
Q.push(dalje);
}
}
if(polje[dalje.x][dalje.y]=='D' && dist[dalje.x][dalje.y]>dist[pos.x][pos.y])
{
dist[dalje.x][dalje.y]=dist[pos.x][pos.y];
Q.push(dalje);
}
else if(dist[dalje.x][dalje.y]>dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0'))
{
dist[dalje.x][dalje.y]=dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0');
Q.push(dalje);
}
}
}
}
}
int main()
{
scanf("%d%d",&a,&b);
while(a!=0 && b!=0)
{
int c=0,d=0,e=0,f=0;
for(int i=0;i<b;++i)scanf("%s",polje[i]);
//scanf("\n");
for(int i=0;i<b;++i)
for(int j=0;j<a;++j)
{
if(polje[i][j]=='S'){c=i;d=j;}
if(polje[i][j]=='D'){e=i;f=j;}
}
bfs(c,d);
printf("%d\n",dist[e][f]);
for(int i=0;i<150;++i)
for(int j=0;j<150;++j){bio[i][j]=false;dist[i][j]=0;}
Q.empty();
scanf("%d%d",&a,&b);
}
return 0;
}
I try some weird test example like :
3 3
S9D
1X1
111
But my program print 5, and that is good. So I need your help.
Mistake was here:
if(polje[dalje.x][dalje.y]!='X' || polje[dalje.x][dalje.y]!='S')
It should be
if(polje[dalje.x][dalje.y]!='X' && polje[dalje.x][dalje.y]!='S')

Knight Tour C++

I am trying to solve Knight Tour Problem using recursive Backtracking. Can someone help me optimize my code. My code works till 6X6 board. . After N=7 it takes almost infinite time to solve .
Here is my code :
#include <iostream>
#include "genlib.h"
#include "grid.h"
#include "vector.h"
#include <iomanip>
const int NOT_VISITED = -1;
//Size of the board
const int N = 6;
const int N2 = N*N;
typedef Grid<int> chess;
struct position{
int row;
int col;
};
//Initializes the board and makes each and every
//square value as NOT_VISITED
void initializeBoard(chess &board)
{
for(int i=0;i<board.numRows();i++)
for(int j=0;j<board.numCols();j++)
board[i][j] = NOT_VISITED;
}
//Returns true if the square is visited;
bool visited(chess &board,position square)
{
return board[square.row][square.col ] != NOT_VISITED;
}
//Returns true if the givien position variable is outside the chess board
bool outsideChess(chess &board, position square)
{
if(square.row <board.numRows() && square.col <board.numCols() && square.row >=0 && square.col >=0)
return false;
return true;
}
void visitSquare(chess &board,position square,int count)
{
board[square.row] [square.col] = count;
}
void unVisitSquare(chess &board,position square)
{
board[square.row] [square.col] = NOT_VISITED;
}
position next(position square,int irow, int icol)
{
square.row += irow;
square.col += icol;
return square;
}
Vector<position> calulateNextSquare(chess board,position square)
{
Vector<position> list;
for(int i=-2;i<3;i=i+4)
{
for(int j=-1;j<2;j=j+2)
{
list.add(next(square,i,j));
list.add(next(square,j,i));
}
}
return list;
}
bool knightTour(chess &board,position square, int count)
{
//cout<<count<<endl;
//Base Case if the problem is solved;
if(count>N2)
return true;
if(outsideChess(board,square))
return false;
//return false if the square is already visited
if(visited(board,square))
return false;
visitSquare(board,square,count);
Vector<position> nextSquareList = calulateNextSquare(board,square);
for(int i=0;i<nextSquareList.size();i++)
if(knightTour(board, nextSquareList[i], count+1))
return true;
unVisitSquare(board,square);
return false;
}
void printChess(chess &board)
{
for(int i=0;i<board.numRows();i++)
{
for(int j=0;j<board.numCols();j++)
cout<<setw(4)<<board[i][j];
cout<<endl;
}
}
int main()
{
chess board(N,N);
initializeBoard(board);
position start;
start.row = 0; start.col = 0;
if(knightTour(board,start,1))
printChess(board);
else
cout<<"Not Possible";
return 0;
}
i am using Stanford 106B Libraries( grid is a 2 dimensional vector )
Visual studio 2008 Blank project with required library files https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0BwLe9NJT8IreNWU0N2M5MGUtY2UxZC00ZTY2LWE1YjQtMjgxYzAxMWE3OWU2&hl=en
I'd say, for a start, get rid of this:
Vector<position> nextSquareList = calulateNextSquare(board,square);
creating a Vector on each step will take a lot of time. You could either use an array (fixed sized, since you know there are 8 possible moves), or unroll the loop entirely. Compare with this version, similar to yours.
Some modifications I would like to suggest:
#include <iostream>
#include "genlib.h"
#include "grid.h"
#include "vector.h"
#include <iomanip>
const int NOT_VISITED = -1;
//Size of the board
const int N = 6;
const int N2 = N*N;
typedef int chess[N][N]; // <------------- HERE
struct position{
int row;
int col;
};
//Initializes the board and makes each and every
//square value as NOT_VISITED
void initializeBoard(chess &board)
{
for(int i=0;i<board.numRows();i++)
for(int j=0;j<board.numCols();j++)
board[i][j] = NOT_VISITED;
}
//Returns true if the square is visited;
bool visited(chess &board,position square)
{
return board[square.row][square.col ] != NOT_VISITED;
}
//Returns true if the givien position variable is outside the chess board
bool outsideChess(chess &board, position square)
{
if(square.row <board.numRows() && square.col <board.numCols() && square.row >=0 && square.col >=0)
return false;
return true;
}
void visitSquare(chess &board,position square,int count)
{
board[square.row] [square.col] = count;
}
void unVisitSquare(chess &board,position square)
{
board[square.row] [square.col] = NOT_VISITED;
}
position next(position square,int irow, int icol)
{
square.row += irow;
square.col += icol;
return square;
}
void calulateNextSquare(chess board,position square, Vector<position>& list) // <------------- HERE
{
// ------------- HERE
//Also, change this part to add only unvisited and not out-of-board positions.
for(int i=-2;i<3;i=i+4)
{
for(int j=-1;j<2;j=j+2)
{
list.add(next(square,i,j));
list.add(next(square,j,i));
}
}
}
bool knightTour(chess &board,position square, int count)
{
//cout<<count<<endl;
//Base Case if the problem is solved;
if(count>N2)
return true;
if(outsideChess(board,square))
return false;
//return false if the square is already visited
if(visited(board,square))
return false;
visitSquare(board,square,count);
Vector<position> nextSquareList; // <------------- HERE
calulateNextSquare(board,square,nextSquareList);
for(int i=0;i<nextSquareList.size();i++)
if(knightTour(board, nextSquareList[i], count+1))
return true;
unVisitSquare(board,square);
return false;
}
void printChess(chess &board)
{
for(int i=0;i<board.numRows();i++)
{
for(int j=0;j<board.numCols();j++)
cout<<setw(4)<<board[i][j];
cout<<endl;
}
}
int main()
{
chess board(N,N);
initializeBoard(board);
position start;
start.row = 0; start.col = 0;
if(knightTour(board,start,1))
printChess(board);
else
cout<<"Not Possible";
return 0;
}
But please note that you still have a exponential complexity, and optimizing your code wont change it.
You are passing a copy of the board to calculateNextSquare but it seems you don't need it in this method.
Also, you return a vector in this method but you should pass it by reference.