-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq2.cpp
More file actions
130 lines (110 loc) · 2.09 KB
/
q2.cpp
File metadata and controls
130 lines (110 loc) · 2.09 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
// 2:A Knight's Journey
// 简单的翻译一下,骑士在矩形棋盘上走,一步只能直走两格再拐一个弯(垂直方向走一格)
// 在A1起点下,棋盘每个点都只经过一次的方案
// A1 坐标为(0,0) 右与下为正方向
bool square[26][26]; // 棋盘
int p = 0; // x,行数,数字表示
int q = 0; // y,列数,字母表示
int maxStep = 0; // 总步数
bool flag = false; // 已经找到方案
struct Node {
int x = 0;
int y = 0;
}route[26];
// 方向:0~7
// !题目要求字典序最小,所以这里要按序排列
void move(int& x, int& y, int direction) {
switch (direction) {
case 0:
x -= 2;
y -= 1;
break;
case 1:
x -= 2;
y += 1;
break;
case 2:
x -= 1;
y -= 2;
break;
case 3:
x -= 1;
y += 2;
break;
case 4:
x += 1;
y -= 2;
break;
case 5:
x += 1;
y += 2;
break;
case 6:
x += 2;
y -= 1;
break;
case 7:
x += 2;
y += 1;
break;
default:
break;
}
}
void DFS(int x, int y, int step) {
if (flag) {
// 在找到字典序最小的方案后快速结束其他正在递归中的函数
// 避免不必要的递归
return;
}
square[x][y] = true;
route[step].x = x;
route[step].y = y;
step++;
if (step == maxStep) {
flag = true;
return;
}
for (int i = 0; i < 8; i++) {
int nextX = x;
int nextY = y;
move(nextX, nextY, i);
if (!square[nextX][nextY] &&
nextX >= 0 && nextX < q &&
nextY >= 0 && nextY < p) {
DFS(nextX, nextY, step);
}
}
// 恢复状态
square[x][y] = false;
}
int main(int argc, char *argv[]) {
int n = 0;
cin >> n;
for (int index = 1; index <= n; index++) {
cin >> p >> q;
maxStep = p * q;
flag = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
square[i][j] = false;
}
}
DFS(0, 0, 0);
cout << "Scenario #" << index << ":" << endl;
if (flag) {
for (int i = 0; i < maxStep; i++) {
char ch = route[i].x + 'A';
cout << ch << route[i].y + 1;
}
cout << endl << endl;
} else {
cout << "impossible" << endl << endl;
}
}
return 0;
}