Cod sursă (job #114311)

Utilizator avatar cstefan Constantin Stefan cstefan IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp | 1,82 kb
Rundă Tema 14 clasele 9-10 2014/15 Status evaluat
Dată 5 feb. 2015 16:37:16 Scor 19
#include <cstdio>
#include <cstdlib>

const int NMAX=22;
const int dx[4]={-2,0,2,0};
const int dy[4]={0,-2,0,2};

int N,M,I,X[2*NMAX],Y[2*NMAX];
bool a[NMAX][NMAX];


int move[NMAX];

int codif(int x,int y,int tip) { return (x<<10) + (y<<5) + tip; }
int decod_x(int move) { return move>>10; }
int decod_y(int move) { return (move>>5)&31; }
int decod_t(int move) { return move&31; }

void back(int k)
{
    int i,j,x,y,t,sh;
    if (k==I)
    {
        for (i=1;i<I;++i)
        {
            x=decod_x(move[i]);
            y=decod_y(move[i]);
            t=decod_t(move[i]);
            printf("%d %d %d %d\n",x,y,x+dx[t],y+dy[t]);
        }
        exit(0);
    }

    for (sh=1;sh<=I;++sh)
    {
        i=X[sh];j=Y[sh];
        if (a[i][j])
            for (t=0;t<4;++t)
                if (0< i+dx[t] && i+dx[t]<=N && 0<j+dy[t] && j+dy[t]<=M)
                    if (!a[i+dx[t]][j+dy[t]])
                        if (a[(2*i+dx[t])/2][(2*j+dy[t])/2])
                        {
                            a[i][j]=0;
                            a[(2*i+dx[t])/2][(2*j+dy[t])/2]=0;
                            a[i+dx[t]][j+dy[t]]=1;
                            move[k]=codif(i,j,t);
                            X[sh]=i+dx[t];Y[sh]=j+dy[t];
                            back(k+1);
                            a[i][j]=1;
                            a[(2*i+dx[t])/2][(2*j+dy[t])/2]=1;
                            a[i+dx[t]][j+dy[t]]=0;
                            X[sh]=i;Y[sh]=j;
                        }
    }
}

int main()
{
    int x,y,i;
    freopen("immortal.in","r",stdin);
    freopen("immortal.out","w",stdout);

    scanf("%d %d %d",&N,&M,&I);
    for (i=1;i<=I;++i)
    {
        scanf("%d %d",&x,&y);
        a[x][y]=1;
        X[i]=x;Y[i]=y;
    }

    back(1);

    return 0;
}