Cod sursă (job #309389)

Utilizator avatar tanasaradu tanasaradu tanasaradu IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp | 3,88 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 iul. 2017 05:59:50 Scor 20
#include <bits/stdc++.h>
#define nmax 21
using namespace std;
ifstream fin("immortal.in");
ofstream fout("immortal.out");
bool sol;
int n,m,nrnemuritori;
struct Patru
{
    int xi,yi,xf,yf;
};
Patru st[nmax*nmax];
int top;
struct Dublu
{
    int ox,oy;
};
vector<Dublu>L[2];
inline void Insert(int i,int j)
{
    Dublu w;
    w.ox=i;
    w.oy=j;
    L[1].push_back(w);
}
void Read()
{
    int i,x,y;
    fin>>n>>m>>nrnemuritori;
    for(i=1; i<=nrnemuritori; i++)
    {
        fin>>x>>y;
        Insert(x,y);
    }
}
inline bool Inauntru(int x,int y)
{
    if(1<=x && x<=n && 1<=y && y<=m)
        return true;
    return false;
}
void Afisare(int top)
{
    int i;
    for(i=1; i<=top; i++)
        fout<<st[i].xi<<" "<<st[i].yi<<" "<<st[i].xf<<" "<<st[i].yf<<"\n";
}
inline bool Find(int i,int j)
{
    int k;
    k=L[1].size();
    for(int pas=0; pas<k; pas++)
        if(L[1][pas].ox==i && L[1][pas].oy==j)
            return true;
    return false;
}
inline void Delete(int i,int j)
{
    int k;
    k=L[1].size();
    for(int pas=0; pas<k; pas++)
        if(L[1][pas].ox==i && L[1][pas].oy==j)
        {
            L[1][pas].ox=L[1][k-1].ox;
            L[1][pas].oy=L[1][k-1].oy;
            L[1].pop_back();
            return ;
        }
}
void Back(int top)
{
    int k,pas;
    Dublu w;
    if(top==nrnemuritori)
    {
        Afisare(top-1);
        sol=true;
    }
    else
    {
        k=L[1].size();
        for(pas=0; pas<k && !sol; pas++)
        {
            w.ox=L[1][pas].ox;
            w.oy=L[1][pas].oy;
                 /// Nord
                 if(Find(w.ox-1,w.oy) && Inauntru(w.ox-2,w.oy) && !Find(w.ox-2,w.oy))
            {
                Insert(w.ox-2,w.oy);
                st[top].xi=w.ox;
                st[top].yi=w.oy;
                st[top].xf=w.ox-2;
                st[top].yf=w.oy;
                ///a[i-1][j]=a[i][j]=false;
                Delete(w.ox-1,w.oy);
                Delete(w.ox,w.oy);
                Back(top+1);
                Delete(w.ox-2,w.oy);
               /// a[i-2][j]=false;
                ///a[i-1][j]=a[i][j]=true;
                Insert(w.ox,w.oy);
                Insert(w.ox-1,w.oy);
            }
            ///Sud
              if(Find(w.ox+1,w.oy) && Inauntru(w.ox+2,w.oy) && !Find(w.ox+2,w.oy))
            {
                Insert(w.ox+2,w.oy);
                st[top].xi=w.ox;
                st[top].yi=w.oy;
                st[top].xf=w.ox+2;
                st[top].yf=w.oy;
                Delete(w.ox+1,w.oy);
                Delete(w.ox,w.oy);
                Back(top+1);
                Delete(w.ox+2,w.oy);
                 Insert(w.ox,w.oy);
                Insert(w.ox+1,w.oy);
            }
            ///Est
              if(Find(w.ox,w.oy+1) && Inauntru(w.ox,w.oy+2) && !Find(w.ox,w.oy+2))
            {
                Insert(w.ox,w.oy+2);
                st[top].xi=w.ox;
                st[top].yi=w.oy;
                st[top].xf=w.ox;
                st[top].yf=w.oy+2;
                Delete(w.ox,w.oy+1);
                Delete(w.ox,w.oy);
                Back(top+1);
                Delete(w.ox,w.oy+2);
                Insert(w.ox,w.oy);
                Insert(w.ox,w.oy+1);
            }
            /// VEST
              if(Find(w.ox,w.oy-1) && Inauntru(w.ox,w.oy-2) && !Find(w.ox,w.oy-2))
            {
                Insert(w.ox,w.oy-2);
                st[top].xi=w.ox;
                st[top].yi=w.oy;
                st[top].xf=w.ox;
                st[top].yf=w.oy-2;
                Delete(w.ox,w.oy-1);
                Delete(w.ox,w.oy);
                Back(top+1);
                Delete(w.ox,w.oy-2);
                Insert(w.ox,w.oy);
                Insert(w.ox,w.oy-1);
            }
        }
    }
}
int main()
{
    Read();
    Back(1);
    fin.close();
    fout.close();
    return 0;
}