Pagini recente »
Istoria paginii runda/2019-12-19-clasa-7-tema-15
|
Istoria paginii runda/2022-10-012-clasa-6-tema-05
|
Borderou de evaluare (job #544022)
|
2016-10-05-test-7-8
|
Cod sursă (job #766462)
Cod sursă (job
#766462)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("immortal.in");
ofstream fout("immortal.out");
struct Cell { int x, y; };
struct Move { Cell a, b; };
int main() {
int m, n, k; fin >> m >> n >> k;
vector mat(m + 1, vector<int>(n + 1));
vector<Cell> imm(k + 1);
for (int i = 1; i <= k; i++) {
fin >> imm[i].x >> imm[i].y;
mat[imm[i].x][imm[i].y] = i;
}
vector<Move> ans(k);
vector<bool> alive(k + 1, true);
function<void(int)> bkt = [&](int pos) {
if (pos == k - 1) {
for (int i = 0; i < k - 1; i++) {
fout << ans[i].a.x << ' ' << ans[i].a.y << ' ';
fout << ans[i].b.x << ' ' << ans[i].b.y << '\n';
}
exit(0);
}
for (int i = 1; i <= k; i++)
if (alive[i]) {
int x = imm[i].x, y = imm[i].y;
if (x > 2 && mat[x - 1][y] && !mat[x - 2][y]) {
int j = mat[x - 1][y];
alive[j] = false; mat[x][y] = 0; mat[x - 1][y] = 0; mat[x - 2][y] = i; imm[i].x = x - 2;
ans[pos] = {{x, y}, {x - 2, y}}; bkt(pos + 1);
alive[j] = true; mat[x][y] = i; mat[x - 1][y] = j; mat[x - 2][y] = 0; imm[i].x = x;
}
if (x < m - 1 && mat[x + 1][y] && !mat[x + 2][y]) {
int j = mat[x + 1][y];
alive[j] = false; mat[x][y] = 0; mat[x + 1][y] = 0; mat[x + 2][y] = i; imm[i].x = x + 2;
ans[pos] = {{x, y}, {x + 2, y}}; bkt(pos + 1);
alive[j] = true; mat[x][y] = i; mat[x + 1][y] = j; mat[x + 2][y] = 0; imm[i].x = x;
}
if (y > 2 && mat[x][y - 1] && !mat[x][y - 2]) {
int j = mat[x][y - 1];
alive[j] = false; mat[x][y] = 0; mat[x][y - 1] = 0; mat[x][y - 2] = i; imm[i].y = y - 2;
ans[pos] = {{x, y}, {x, y - 2}}; bkt(pos + 1);
alive[j] = true; mat[x][y] = i; mat[x][y - 1] = j; mat[x][y - 2] = 0; imm[i].y = y;
}
if (y < n - 1 && mat[x][y + 1] && !mat[x][y + 2]) {
int j = mat[x][y + 1];
alive[j] = false; mat[x][y] = 0; mat[x][y + 1] = 0; mat[x][y + 2] = i; imm[i].y = y + 2;
ans[pos] = {{x, y}, {x, y + 2}}; bkt(pos + 1);
alive[j] = true; mat[x][y] = i; mat[x][y + 1] = j; mat[x][y + 2] = 0; imm[i].y = y;
}
}
};
bkt(0);
}