Pagini recente »
Monitorul de evaluare
|
Atașamentele paginii Clasament 2024-04-12-clasa-5-tema-38
|
2020-12-17-clasa-5-tema-13
|
Rating Zarafu Mihnea (Mihnea769)
|
Cod sursă (job #238915)
Cod sursă (job
#238915)
#include <fstream>
using namespace std;
ifstream fin ("hex.in");
ofstream fout ("hex.out");
struct perechi
{
int x, y, mx;
bool clr;
}lee[250001];
int a[501][501], x, y, n, ind, mx = 1000000000;
void ok (int i, int j, int clr, int px)
{
if (i > n || j > n || i < 1 || j < 1)return;
if(a[i][j] == -1)return;
if (a[i][j]%2 != clr)return;
lee[++ind].x=i;
lee[ind].y=j;
lee[ind].clr = clr;
lee[ind].mx = max(px, a[i][j]);
a[i][j]=-1;
return;
}
int main()
{
fin >> n;
for (int i =1; i<= n*n; i++)
{
fin >> x >> y;
a[x][y]=i;
}
for (int i = 1; i <=n; i++)
{
if(a[1][i]%2 == 0)//e albastru
{
lee[++ind].x=1;
lee[ind].y=i;
lee[ind].clr = 0;// 0 = albastru
lee[ind].mx = i;
a[1][i] = -1;
}
if(a[i][1]%2 == 1)//e rosu
{
lee[++ind].x=i;
lee[ind].y=1;
lee[ind].clr = 1; //1 = rosu
lee[ind].mx = i;
a[i][1] = -1;
}
}
for (int i =1; i<=ind; i++)
{
x = lee[i].x;
y = lee[i].y;
if (lee[i].clr == 0 && lee[i].x == n)mx = min(mx, lee[i].mx);
else if (lee[i].clr == 1 && lee[i].y == n)mx = min(mx, lee[i].mx);
else
{
ok(x + 1, y, lee[i].clr, lee[i].mx);
ok(x, y + 1, lee[i].clr, lee[i].mx);
ok(x + 1, y - 1, lee[i].clr, lee[i].mx);
if (lee[i].clr == 0)ok(x, y - 1, lee[i].clr, lee[i].mx);
else ok(x - 1, y + 1, lee[i].clr, lee[i].mx);
}
}
fout << mx;
return 0;
}