Pagini recente » Cod sursa (job #1225427) | Cod sursa (job #3004417) | Cod sursa (job #2842604) | Cod sursa (job #2784795) | Cod sursa (job #2836898)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("matrice2.in");
ofstream fout ("matrice2.out");
int n , q;
/**
9 5 1 8 7
2 1 1 3 8
9 3 9 4 6
4 1 8 6 7
2 4 5 5 6
*/
int a[305][305];
int dx[] = {0 , 0, -1,1};
int dy[] = {-1, 1, 0,0};
struct Elem
{
int cost, x, y;
bool operator < (const Elem A) const
{
return cost < A.cost;
}
};
void Bordare ()
{
for (int i = 0; i <= n + 1; i++)
{
a[i][0] = a[i][n + 1] = 2e9;
a[0][i] = a[n + 1][i] = 2e9;
}
}
int Vecin (int x1, int y1, int x2, int y2)
{
if (abs (x1 - x2) + abs(y1 - y2) == 1)
return 1;
return 0;
}
void Solve (int x1, int y1, int x2, int y2)
{
bitset <305> viz[305];
priority_queue < Elem > q;
int cost_min = a[x1][y1];
q.push({a[x1][y1], x1, y1});
viz[x1][y1] = 1;
while (q.top().x != x2 || q.top().y != y2)
{
int x = q.top().x;
int y = q.top().y;
if (Vecin(x, y, x2, y2))
{
cost_min = min (cost_min, a[x2][y2]);
break;
}
q.pop();
cost_min = min (cost_min, a[x][y]);
for (int k = 0; k < 4; k++)
{
int i = x + dx[k];
int j = y + dy[k];
if (!viz[i][j] && a[i][j] != 2e9)
q.push({a[i][j], i , j});
viz[i][j] = 1;
}
}
fout << cost_min << "\n";
}
int main()
{
fin >> n >> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
fin >> a[i][j];
Bordare();
while (q--)
{
int x1 , y1, x2, y2;
fin >> x1 >> y1 >> x2 >> y2;
Solve(x1, y1, x2 , y2);
}
return 0;
}