Cod sursă (job #766462)

Utilizator avatar AnAverageTurtle Visan Mihnea Alexandru AnAverageTurtle IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp-32 | 2,49 kb
Rundă Arhiva de probleme Status evaluat
Dată 5 mar. 2024 08:39:24 Scor 0
#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);
}