Pagini recente »
Monitorul de evaluare
|
Atașamentele paginii 2016-03-29-clasa-6-tema-24
|
Istoria paginii utilizator/aokirisaki
|
Atașamentele paginii Baraj Șumen 2015 - Seniori
|
Cod sursă (job #238923)
Cod sursă (job
#238923)
#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++)
{
for (int j = 1; j<=n; j++)fout << a[i][j] << ' ';
fout << '\n';
}*/
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 = a[1][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 = a[i][1];
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;
}