Cod sursă (job #357780)

Utilizator avatar cozmacatalin Cozma Catalin cozmacatalin IP ascuns
Problemă Hex Compilator cpp | 2,95 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 feb. 2018 06:01:57 Scor 0
#include <bits/stdc++.h>

using namespace std;

const int MAX = 501;

const int dx[] = {-1,-1,0,0,1,1};
const int dy[] = {0,1,-1,1,-1,0};

char Situation[MAX][MAX];

int N;

struct point
{
    short int x;
    short int y;
};

point T[MAX][MAX];


point root(short int x , short int y)
{
    point  R;

    if(T[x][y].x == 0 && T[x][y].y== 0)
    {
        R.x = x;
        R.y = y;
        return R;
    }

    R = root(T[x][y].x,T[x][y].y);
    T[x][y] = R;
    return R;
}

inline bool Inside(int x , int y)
{
    if ( x < 1 || y < 1 || x > N || y > N);
    return false;

    return true;
}



int main()
{
    freopen("hex.in","r",stdin);
    freopen("hex.out","w", stdout);

    point R1,R2;
    int x,y,S;
    bool Done = false;
    int N;

    scanf("%d", &N);

    for (int i = 0 ; i <= N ; ++i)
        for ( int j = 0; j <= N ; ++j)
        T[i][j].x = T[i][j].y = 0;

    int countt = 1;

    while ( countt <= N*N && !Done)
    {
        scanf("%d%d", &x,&y);

        if(countt%2 == 1) S = 1;
        else S = 2;

        Situation[x][y] = S;

        R1 = root(x,y);

        for ( int direction = 0 ; direction < 6 ; ++direction)
        {
            int x_next = x + dx[direction];
            int y_next = y + dy[direction];

            if(Inside(x_next,y_next)==true && Situation[x_next][y_next] == S)
            {
                R2 = root(x_next,y_next);

                if(R1.x != R2.x || R1.y != R2.y)
                    T[R2.x][R2.y] = R1;
            }
        }

        if( S == 1)

        {
            if( y == 1)
            {
                R2 = root(1,0);
                if( R1.x != R2.x || R1.y != R2.y)
                {
                    T[R1.x][R1.y].x = R2.x;
                    T[R1.x][R1.y].y = R2.y;
                }
            }

            if( y == N)
            {
                R2 = root(2,0);
                if(R1.x != R2.x || R1.y != R2.y)
                {
                    T[R1.x][R1.y].x = R2.x;
                    T[R1.x][R1.y].y = R2.y;
                }
            }

            if(root(1,0).x == root(2,0).x && root(1,0).y == root(2,0).y)
                Done = true;
        }

        if( S == 2)

        {
            if( x == 1)
            {
                R2 = root(0,1);
                if( R1.x != R2.x || R1.y != R2.y)
                {
                    T[R1.x][R1.y].x = R2.x;
                    T[R1.x][R1.y].y = R2.y;
                }
            }

            if( x == N)
            {
                R2 = root(0,2);
                if(R1.x != R2.x || R1.y != R2.y)
                {
                    T[R1.x][R1.y].x = R2.x;
                    T[R1.x][R1.y].y = R2.y;
                }
            }

            if(root(0,1).x == root(0,2).x && root(0,1).y == root(0,2).y)
                Done = true;
        }

        ++countt;
    }

    printf("%d", countt-1);
}