-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1298.cpp
64 lines (59 loc) · 1.74 KB
/
1298.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
//This solution uses Warnsdorff's Rule
//use G++ or Clang++ as compiler, Visual C++ doesn't support "x ... y" in switch-case
#include <iostream>
#include <cstring>
using namespace std;
short N;
bool flag;
int sqr_visited[9][9];
short board[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
void dfs(int r, int c, int depth){
if (depth < N*N) {
for (auto &i : board) { //for every element in board
int x = r + i[0];
int y = c + i[1];
if (x >= 1 && x <= N && y >= 1 && y <= N && !sqr_visited[x][y]) {
sqr_visited[x][y] = depth + 1;
dfs(x, y, depth + 1);
if (!flag) {
sqr_visited[x][y] = 0;
}
else
break;//exit
}
}
}
else{
flag = true;
}
}
int main(){
cin >> N;
memset(sqr_visited, 0, sizeof(sqr_visited));
switch (N) {
case 1:
cout<<"a1";
break;
case 2 ... 4:
cout<<"IMPOSSIBLE";
break;
case 5 ... 8:
flag = false;
sqr_visited[1][1] = 1;
dfs(1, 1, 1);
if (flag) {
for (int i = 1; i <= N * N; ++ i) {
for (int j = 1; j <= N; ++ j) {
for (int k = 1; k <= N; ++ k) {
if (sqr_visited[j][k] == i) {
cout << (char)(j + 'a' - 1) << k << endl;
}
}
}
}
} else {
cout<<"IMPOSSIBLE";
}
}
return 0;
}