Cod sursă (job #420638)

Utilizator avatar Artanis Petrea Calin Artanis IP ascuns
Problemă Hex Compilator cpp | 1,32 kb
Rundă lasm_15_01_cl11_12 Status evaluat
Dată 16 ian. 2019 19:39:06 Scor 90
#include <bits/stdc++.h>
using namespace std;

int n, i, j, x, y, rx, ry, t[250010];
int dx[6] = {0, 1, 0, - 1, 1, - 1};
int dy[6] = {1, 0, - 1, 0, - 1, 1};
char a[510][510];
bool w[4][250010];

int rad(int x)
{
    if (t[x] == 0) return x;
    return t[x] = rad(t[x]);
}
int main()
{
    ifstream cin("hex.in");
    ofstream cout("hex.out");
    cin >> n;
    for (i = 1; i <= n * n; ++i)
    {
        cin >> x >> y;
        rx = rad(x * (n + 1) + y);
        if (x == 1)
            w[0][rx] = 1;
        if (x == n)
            w[1][rx] = 1;
        if (y == 1)
            w[2][rx] = 1;
        if (y == n)
            w[3][rx] = 1;
        a[x][y] = (i % 2) + 'a';
        for (j = 0; j < 6; ++j)
            if (a[x + dx[j]][y + dy[j]] == a[x][y])
            {
                ry = rad((x + dx[j]) * (n + 1) + y + dy[j]);
                if (rx != ry)
                {
                    t[ry] = rx;
                    for (int l = 0; l < 4; ++ l)
                        if (w[l][ry])
                            w[l][rx] = 1;
                        else if (w[l][rx])
                            w[l][ry] = 1;
                }
            }
        if ((i % 2 == 0 && w[0][rx] && w[1][rx]) || (i % 2 && w[2][rx] && w[3][rx]))
        {
        	cout << i;
			return 0;
		}
    }
}