boolIn(int x, int y)// 判断是否在棋盘内 { if (x < 0 || x >= 8 || y < 0 || y >= 8) returnfalse; else returntrue; }
booldfs(int x, int y, int step)// 暴力搜索 { if (step == 64) returntrue; vector<array<int, 3>> v; for (int i = 0; i < 8; i++) { int nowx = x + dx[i]; int nowy = y + dy[i]; if ((!In(nowx, nowy)) || a[nowx][nowy] != 0) continue; else{ a[nowx][nowy] = step + 1; if(dfs(nowx, nowy, step + 1)) returntrue; a[nowx][nowy] = 0; }
} returnfalse; }
voidsolve() { int x, y; cin >> x >> y; a[x][y] = 1; dfs(x, y, 1); for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { cout << a[i][j] << " "; } cout << a[i][7] << endl; } }
boolIn(int x, int y) { if (x < 0 || x >= 8 || y < 0 || y >= 8) returnfalse; else returntrue; }
intget_weight(int x, int y){ int temp_x, temp_y, weight = 0; for (int i = 0; i < 8; i++) { temp_x = x + dx[i], temp_y = y + dy[i]; if (In(temp_x, temp_y) && a[temp_x][temp_y] == 0) weight++; } return weight; }
booldfs(int x, int y, int step) { // cout << x << " " << y << endl; if (step == 64) returntrue; // cout << x << " " << y << endl; vector<array<int, 3>> v; for (int i = 0; i < 8; i++) { int nowx = x + dx[i]; int nowy = y + dy[i]; if ((!In(nowx, nowy)) || a[nowx][nowy] != 0) continue; else{ v.push_back({get_weight(nowx, nowy), nowx, nowy}); }
} sort(v.begin(), v.end()); for (auto temp: v){ int nowx = temp.at(1), nowy = temp.at(2); // cout << nowx << " " << nowy << endl; a[nowx][nowy] = step + 1; if (dfs(nowx, nowy, step + 1)) returntrue; a[nowx][nowy] = 0; } returnfalse; }
voidsolve() { int x, y; cin >> x >> y; a[x][y] = 1; dfs(x, y, 1); for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { cout << a[i][j] << " "; } cout << a[i][7] << endl; } // checkAns(x, y); }