Pagini recente » Profil ElefantulCici | Cod sursa (job #1446162) | Cod sursa (job #2950885) | Cod sursa (job #3120751)
#include <bits/stdc++.h>
using namespace std;
ifstream in ("matrice2.in");
ofstream out ("matrice2.out");
const int NMAX = 300;
const int dx[] = {-1, +1, 0, 0};
const int dy[] = {0, 0, -1, +1};
int n;
int a[NMAX + 5][NMAX + 5];
bitset<NMAX + 5>vis[NMAX + 5];
bool check (int x1, int y1, int x2, int y2, int v)
{
if (a[x1][y1] < v)
return false;
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
vis[i][j] = false;
vis[x1][y1] = true;
queue<pair<int, int>>q;
q.push({x1, y1});
while (!q.empty())
{
auto [x, y] = q.front();
q.pop();
for (int k=0; k<4; k++)
{
int i = x + dx[k], j = y + dy[k];
if (i > 0 && j > 0 && i <= n && j <= n && !vis[i][j] && a[i][j] >= v)
{
q.push({i, j}), vis[i][j] = true;
}
}
}
return vis[x2][y2];
}
int main()
{
int q;
in >> n >> q;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
in >> a[i][j];
}
}
while (q--)
{
int x1, y1, x2, y2;
in >> x1 >> y1 >> x2 >> y2;
int l=1, r=1e9;
while (l < r)
{
int mid = (l + r + 1) / 2;
if (check(x1, y1, x2, y2, mid))
l = mid;
else
r = mid - 1;
}
out << r << '\n';
}
return 0;
}