-
Notifications
You must be signed in to change notification settings - Fork 3
/
chess.cpp
72 lines (66 loc) · 1.39 KB
/
chess.cpp
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
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
#define ROW 5
#define COL 5
bool traverse(vector<vector<int> > &result, vector<vector<int> > &board, int x, int y, int count)
{
board[x][y] = count;
if(count >= ROW * COL)
{
result = board;
return true;
}
int dy[8] = {2, 2, 1, -1, -2, -2, -1, 1}; //
int dx[8] = {-1, 1, 2, 2, 1, -1, -2, -2}; //
for(int i = 0; i < 8; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx <0 || nx >= COL || ny < 0 || ny >= ROW)
{
continue;
}
if(board[nx][ny] != 0) // already traversed
{
continue;
}
//go next
traverse(result, board, nx, ny, count+1);
}
cout<<"Test:["<<count<<"]" << x << " "<<y<<endl;
board[x][y] = 0;
return true;
}
void printBoard(vector<vector<int> > &board)
{
for(int i = 0; i < board.size(); i++)
{
for(int j = 0; j < board[i].size(); j++)
{
cout<<setw(4)<<board[i][j]<< " ";
}
cout<<endl;
}
}
int main(void)
{
vector<vector<int> > board(ROW, vector<int>(COL, 0));
vector<vector<int> > result(ROW, vector<int>(COL, 0));
int start_x = 0;
int start_y = 0;
cout<<"Please input start point:"<<endl;
cin>>start_x>>start_y;
cout<<"start from ["<<start_x<<","<<start_y<<"]"<<endl;
bool ok = traverse(result, board, start_x, start_y, 1);
if(ok)
{
printBoard(result);
}
else
{
cout<<"Unable to traverse all cells"<<endl;
}
return 0;
}