Cod sursă (job #114313)

Utilizator avatar cstefan Constantin Stefan cstefan IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp | 3,78 kb
Rundă Tema 14 clasele 9-10 2014/15 Status evaluat
Dată 5 feb. 2015 16:42:05 Scor 72
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
const string problemName = "immortal";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int N, M, K, solution, mlc;
vector<pair<PII, PII> > S;
set<PII> V;
bool A[22][22];

void back() {
    if(V.size() == 1) {
        solution = 1;
        for(auto x : S)
            printf("%d %d %d %d\n", x.first.first, x.first.second, x.second.first, x.second.second);
        return;
    }

    for(auto it = V.begin(); it != V.end() && !solution && (mlc % 2); it++) {
        for(auto i = 0; i < 4; i++) {
            auto a = it->first + dx[i];
            auto b = it->second + dy[i];

            if(A[a][b]) {
                auto y = make_pair(a, b);

                a += dx[i];
                b += dy[i];

                auto p = make_pair(a, b);

                if(1 <= a && a <= N && 1 <= b && b <= M && !A[p.first][p.second]) {
                    auto _V = V;

                    _V.erase(*it);
                    _V.erase(y);
                    _V.insert(p);

                    A[it->first][it->second] = 0;
                    A[y.first][y.second] = 0;
                    A[p.first][p.second] = 1;

                    S.push_back(make_pair(*it, p));
                    swap(V, _V);
                    back();
                    swap(V, _V);
                    S.pop_back();

                    A[it->first][it->second] = 1;
                    A[y.first][y.second] = 1;
                    A[p.first][p.second] = 0;
                }
            }
        }
    }

    for(auto it = V.rbegin(); it != V.rend() && !solution && !(mlc % 2); it++) {
        for(auto i = 0; i < 4; i++) {
            auto a = it->first + dx[i];
            auto b = it->second + dy[i];

            if(A[a][b]) {
                auto y = make_pair(a, b);

                a += dx[i];
                b += dy[i];

                auto p = make_pair(a, b);

                if(1 <= a && a <= N && 1 <= b && b <= M && !A[p.first][p.second]) {
                    auto _V = V;

                    _V.erase(*it);
                    _V.erase(y);
                    _V.insert(p);

                    A[it->first][it->second] = 0;
                    A[y.first][y.second] = 0;
                    A[p.first][p.second] = 1;

                    S.push_back(make_pair(*it, p));
                    swap(V, _V);
                    back();
                    swap(V, _V);
                    S.pop_back();

                    A[it->first][it->second] = 1;
                    A[y.first][y.second] = 1;
                    A[p.first][p.second] = 0;
                }
            }
        }
    }
}

int main() {
    int i, x, y;

    srand(time(NULL));
    mlc = rand() % 5;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d%d%d", &N, &M, &K);

    for(i = 1; i <= K; i++) {
        scanf("%d%d", &x, &y);
        V.insert(make_pair(x, y));
        A[x][y] = 1;
    }

    back();

    return 0;
}