language: C++ 4.7.2 (gcc-4.7.2)
date: 592 days 23 hours ago
link:
visibility: public
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
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
const int NOT_VISITED = -1;
//Size of the board
const int N = 7;
const int N2 = N*N;
 
typedef int chess[N][N];
 
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<N;i++)
        for(int j=0;j<N;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 <N && square.col <N && 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.push_back(next(square,i,j));
            list.push_back(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<N;i++)
    {
        for(int j=0;j<N;j++)
            cout<<setw(4)<<board[i][j];
        cout<<endl;
    }
}
 
 
int main()
{
    chess board;
    initializeBoard(board);
    position start;
    start.row = 0; start.col = 0;
    if(knightTour(board,start,1))
        printChess(board);
    else
        cout<<"Not Possible";
    return 0;
}
prog.cpp: In function ‘bool knightTour(int (&)[7][7], position, int)’:
prog.cpp:84: warning: comparison between signed and unsigned integer expressions