Cod sursă (job #309388)

Utilizator avatar tanasaradu tanasaradu tanasaradu IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp | 3,11 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 iul. 2017 05:59:07 Scor 40
#include <bits/stdc++.h>
#define nmax 21
using namespace std;
ifstream fin("immortal.in");
ofstream fout("immortal.out");
bool a[nmax][nmax],sol;
int n,m,nrnemuritori;
struct Patru
{
    int xi,yi,xf,yf;
};
Patru st[nmax*nmax];
int top;
void Read()
{
    int i,x,y;
    fin>>n>>m>>nrnemuritori;
    for(i=1;i<=nrnemuritori;i++)
    {
        fin>>x>>y;
        a[x][y]=true;
    }
}
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";
}
void Back(int top)
{
    if(top==nrnemuritori)
    {
        Afisare(top-1);
        sol=true;
    }
    else
    {
        for(int i=1;i<=n && !sol;i++)
                for(int j=1;j<=m && !sol;j++)
                    if(a[i][j])/// am gasit un posibil "nemuritor"
                {
                    /// Nord
                    if(a[i-1][j] && Inauntru(i-2,j) && !a[i-2][j])
                    {
                        a[i-2][j]=true;
                        st[top].xi=i;
                        st[top].yi=j;
                        st[top].xf=i-2;
                        st[top].yf=j;
                        a[i-1][j]=a[i][j]=false;
                        Back(top+1);
                         a[i-2][j]=false;
                        a[i-1][j]=a[i][j]=true;
                    }
                    ///Sud
                    if(a[i+1][j] && Inauntru(i+2,j) && !a[i+2][j])
                    {
                         a[i+2][j]=true;
                        st[top].xi=i;
                        st[top].yi=j;
                        st[top].xf=i+2;
                        st[top].yf=j;
                        a[i+1][j]=a[i][j]=false;
                        Back(top+1);
                         a[i+2][j]=false;
                        a[i+1][j]=a[i][j]=true;
                    }
                    ///Est
                    if(a[i][j+1] && Inauntru(i,j+2) && !a[i][j+2])
                    {
                         a[i][j+2]=true;
                        st[top].xi=i;
                        st[top].yi=j;
                        st[top].xf=i;
                        st[top].yf=j+2;
                        a[i][j+1]=a[i][j]=false;
                        Back(top+1);
                         a[i][j+2]=false;
                        a[i][j+1]=a[i][j]=true;
                    }
                    /// VEST
                      if(a[i][j-1] && Inauntru(i,j-2) && !a[i][j-2])
                    {
                         a[i][j-2]=true;
                        st[top].xi=i;
                        st[top].yi=j;
                        st[top].xf=i;
                        st[top].yf=j-2;
                        a[i][j-1]=a[i][j]=false;
                        Back(top+1);
                         a[i][j-2]=false;
                        a[i][j-1]=a[i][j]=true;
                    }
                }
    }
}
int main()
{
    Read();
    Back(1);
    fin.close();
    fout.close();
    return 0;
}