Cod sursă (job #286467)

Utilizator avatar danyvs Sava Daniel danyvs IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp | 2.21 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 feb. 2017 20:51:56 Scor 50
#include <fstream>
#include <algorithm>

using namespace std;

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

const int NMAX = 25;

struct battle {
    int xs, ys, xf, yf;
};

int n, m, k;
bool A[NMAX][NMAX];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
battle vec[NMAX * NMAX];

void read() {
    fin >> n >> m >> k;
    for (int i = 0; i < k; ++ i) {
        int x, y;
        fin >> x >> y;
        A[x][y] = true;
    }
}

void print() {
    for (int i = 0; i < k - 1; ++ i)
        fout << vec[i].xs << " " << vec[i].ys << " " << vec[i].xf << " " << vec[i].yf << "\n";
}

void bkt(int key) {
    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= m; ++ j)
            if (A[i][j]) {
                // daca am gasit un nemuritor
                for (int it = 0; it < 4; ++ it) {
                    int x = i + dx[it];
                    int y = j + dy[it];
                    if (A[x][y]) {
                        // daca langa el se afla un alt nemuritor
                        int l = x + dx[it];
                        int c = y + dy[it];
                        if (l > 0 && l <= n && c > 0 && c <= m && !A[l][c]) {
                        // daca lupta poate avea loc
                        A[i][j] = false; // nemuritorul atactor isi paraseste pozitia
                        A[x][y] = false; // nemuritorul pierzator este eliminat
                        A[l][c] = true; // noua pozitie a nemuritorului atacator
                        // se memoreaza batalia
                        vec[key].xs = i;
                        vec[key].ys = j;
                        vec[key].xf = l;
                        vec[key].yf = c;
                        if (key == k - 2) {
                            print();
                            exit(EXIT_SUCCESS);
                        }
                        else
                            bkt(key + 1);
                        A[i][j] = true;
                        A[x][y] = true;
                        A[l][c] = false;
                        }
                    }
                }
            }
}

int main() {
    read();
    fin.close();
    bkt(0);
    fout.close();
    return 0;
}