Cod sursă (job #800850)

Utilizator avatar bazg Yanıp-küygen yarıqlar bazg IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp-32 | 2,42 kb
Rundă Arhiva de probleme Status evaluat
Dată 24 dec. 2024 23:37:04 Scor 10
#include <bits/stdc++.h>
using namespace std;

ifstream fin("immortal.in");
ofstream fout("immortal.out");

bool found = false;

void back(vector<vector<int>>& a, vector<pair<int, int>>& activeCells, vector<vector<int>>& raspuns, int I, int n, int m, int k) {
    if (found) return;

    if (I == 1) {
        found = true;
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < 4; j++)
                fout << raspuns[i][j] << " ";
            fout << "\n";
        }
        return;
    }

    for (int idx = 0; idx < activeCells.size(); idx++) {
        int i = activeCells[idx].first, j = activeCells[idx].second;
        if (a[i][j] == 1) {
            // up
            if (i - 2 > 0 && a[i - 2][j] == 0 && a[i - 1][j] == 1) {
                a[i][j] = 0; a[i - 1][j] = 0; a[i - 2][j] = 1;
                raspuns[k] = {i, j, i - 2, j};
                activeCells.push_back({i - 2, j});
                back(a, activeCells, raspuns, I - 1, n, m, k + 1);
                activeCells.pop_back();
                a[i][j] = 1; a[i - 1][j] = 1; a[i - 2][j] = 0;
            }
            // left
            if (j - 2 > 0 && a[i][j - 2] == 0 && a[i][j - 1] == 1) {
                a[i][j] = 0; a[i][j - 1] = 0; a[i][j - 2] = 1;
                raspuns[k] = {i, j, i, j - 2};
                activeCells.push_back({i, j - 2});
                back(a, activeCells, raspuns, I - 1, n, m, k + 1);
                activeCells.pop_back();
                a[i][j] = 1; a[i][j - 1] = 1; a[i][j - 2] = 0;
            }
            // right
            if (j + 2 <= m && a[i][j + 2] == 0 && a[i][j + 1] == 1) {
                a[i][j] = 0; a[i][j + 1] = 0; a[i][j + 2] = 1;
                raspuns[k] = {i, j, i, j + 2};
                activeCells.push_back({i, j + 2});
                back(a, activeCells, raspuns, I - 1, n, m, k + 1);
                activeCells.pop_back();
                a[i][j] = 1; a[i][j + 1] = 1; a[i][j + 2] = 0;
            }
        }
    }
}

int main() {
    int n, m, I;
    fin >> n >> m >> I;
    vector<vector<int>> a(n + 1, vector<int>(m + 1, 0));
    vector<vector<int>> raspuns(I, vector<int>(4, 0));
    vector<pair<int, int>> activeCells;

    for (int i = 1; i <= I; i++) {
        int a1, a2;
        fin >> a1 >> a2;
        a[a1][a2] = 1;
        activeCells.emplace_back(a1, a2);
    }

    back(a, activeCells, raspuns, I, n, m, 0);
    return 0;
}