QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#662581 | #5641. Magical Mystery Knight's Tour | nero1342 | 0 | 13ms | 3752kb | C++14 | 4.0kb | 2024-10-21 05:36:35 | 2024-10-21 05:36:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int col[8], row[8], a[8][8], cr[8], cc[8], cfull = 0, rfull = 0;
pair<int, int> found[66];
bool check() {
bool flag = cfull == 8 && rfull == 8;
if (flag) {
for (int ii = 0; ii < 8; ++ii) {
for (int jj = 0; jj < 8; ++jj) {
cout << fixed << setw(2) << a[ii][jj] << ' ';
}
// cout << '\n';
cout << '\n';
}
return true;
}
return false;
}
const int dx[] = {2, 2, 1, -1, -2, -2, -1, 1};
const int dy[] = {1, -1, -2, -2, -1, 1, 2, 2};
vector<pair<int, int>> e[8][8];
// unordered_set<pair<int, int>> e[8][8];
// vector<pair<int, int>> e2[8][8];
int last(int x) {
int r = 64, l = 64 - x + 1;
return (l + r) * (r - l + 1) / 2;
}
int next(int x, int idx) {
int l = idx, r = idx + x - 1;
return (l + r) * (r - l + 1) / 2;
}
bool Go(int x, int y, int idx) {
if (row[x] + last(8 - cr[x]) < 260) return false;
if (col[y] + last(8 - cc[y]) < 260) return false;
// if (row[x] + next(8 - cr[x], idx + 1) > 260) return false;
// if (col[y] + next(8 - cc[y], idx + 1) > 260) return false;
if (idx == 64) {
if (check()) return true;
return false;
}
if (found[idx + 1].first == -1) {
for (auto [xx, yy] : e[x][y]) {
if (a[xx][yy] == -1) {
if (row[xx] + idx + 1 > 260) continue;
if (col[yy] + idx + 1 > 260) continue;
++cr[xx];
++cc[yy];
a[xx][yy] = idx + 1;
row[xx] += idx + 1;
col[yy] += idx + 1;
if (cr[xx] == 8 && row[xx] == 260) ++rfull;
if (cc[yy] == 8 && col[yy] == 260) ++cfull;
if (Go(xx, yy, idx + 1)) return true;
if (cr[xx] == 8 && row[xx] == 260) --rfull;
if (cc[yy] == 8 && col[yy] == 260) --cfull;
a[xx][yy] = -1;
row[xx] -= idx + 1;
col[yy] -= idx + 1;
--cr[xx];
--cc[yy];
}
}
} else {
int xx = found[idx + 1].first - x;
int yy = found[idx + 1].second - y;
if (abs(xx * yy) != 2) return false;
if (Go(found[idx + 1].first, found[idx + 1].second, idx + 1)) return true;
}
return false;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
double stime = clock();
int test; cin >> test;
for (int i = 0; i < test; ++i) {
int k; cin >> k; cout << k << '\n';
int si, sj;
cfull = rfull = 0;
for (int i = 0; i < 8; ++i) {
row[i] = col[i] = cr[i] = cc[i] = 0;
}
for (int i = 1; i <= 64; ++i) found[i] = {-1, -1};
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
cin >> a[i][j];
if (a[i][j] != -1) {
row[i] += a[i][j];
col[j] += a[i][j];
found[a[i][j]] = {i, j};
cr[i]++;
cc[j]++;
}
if (a[i][j] == 1) {
si = i, sj = j;
}
}
}
for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; ++y) {
e[x][y].clear();
for (int i = 0; i < 8; ++i) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8) continue;
if (a[xx][yy] == -1) {
e[x][y].push_back({xx, yy});
}
}
// random_shuffle(e[x][y].begin(), e[x][y].end());
}
}
for (int i = 0; i < 8; ++i) {
if (cr[i] == 8 && row[i] == 260) rfull += 1;
if (cc[i] == 8 && col[i] == 260) cfull += 1;
}
Go(si, sj, 1);
}
// cerr << "Running time: " << (clock() - stime) / CLOCKS_PER_SEC << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 0
Wrong Answer
time: 13ms
memory: 3752kb
input:
50 1 1 48 -1 -1 33 -1 63 18 30 51 -1 3 -1 -1 -1 -1 -1 -1 -1 -1 15 -1 -1 -1 -1 -1 -1 45 -1 -1 36 -1 -1 -1 25 -1 9 -1 21 60 -1 -1 -1 -1 24 57 12 -1 -1 6 -1 -1 39 -1 -1 -1 54 -1 42 -1 -1 -1 -1 -1 2 18 -1 10 -1 -1 31 42 -1 11 -1 -1 -1 -1 -1 51 30 -1 17 -1 -1 32 -1 -1 43 -1 12 61 20 5 -1 -1 52 -1 -1...
output:
1 1 48 31 50 33 16 63 18 30 51 46 3 62 19 14 35 47 2 49 32 15 34 17 64 52 29 4 45 20 61 36 13 5 44 25 56 9 40 21 60 28 53 8 41 24 57 12 37 43 6 55 26 39 10 59 22 54 27 42 7 58 23 38 11 2 18 63 10 39 50 31 42 7 11 38 19 62 41 8 51 30 64 17 40 9 32 49 6 43 37 12 61 20 5 44 29 ...
result:
wrong answer 256th lines differ - expected: '42 31 8 49 4 55 46 25', found: '42 31 8 49 4 25 46 55 '