Cod sursa(job #2154342)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 6 martie 2018 21:18:43
Problema Matrice 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <bits/stdc++.h>
#define Nmax 305
#define DIM 70000
using namespace std;
int poz=DIM-1;
char buffer[DIM];
void read(int &x)
{
    x=0;
    while(!isdigit(buffer[poz]))
        if(++poz==DIM)
        {
            poz=0;
            fread(buffer,1,DIM,stdin);
        }
    while(isdigit(buffer[poz]))
    {
        x=x*10+buffer[poz]-'0';
        if(++poz==DIM)
        {
            poz=0;
            fread(buffer,1,DIM,stdin);
        }
    }
}
int a[Nmax][Nmax];
int M[Nmax][Nmax];
class cmp
{
public:
    bool operator () (const pair <int,int> &lsh, const pair <int,int> &rsh) const
    {
        return a[lsh.first][lsh.second]<a[rsh.first][rsh.second];
    }
};
priority_queue < pair <int,int>, vector <pair <int,int> >, cmp> pq;
int x1,y1,x2,y2;
const int dx[]={0,0,-1,1};
const int dy[]={-1,1,0,0};
void BFS()
{
    while(!pq.empty()) pq.pop();
    memset(M,0,sizeof(M));
    int i,j,iu,ju,k;
    pq.push({x1,y1});
    M[x1][y1]=a[x1][y1];
    while(!pq.empty())
    {
        i=pq.top().first;
        j=pq.top().second;
        pq.pop();
        for(k=0;k<4;k++)
        {
            iu=i+dx[k];
            ju=j+dy[k];
            if(a[iu][ju] and !M[iu][ju])
            {
                if(iu==x2 and ju==y2)
                {
                    printf("%d\n",min(M[i][j],a[iu][ju]));
                    return;
                }
                else
                {
                    pq.push({iu,ju});
                    M[iu][ju]=min(M[i][j],a[iu][ju]);
                }
            }
        }
    }
}
int main()
{
    freopen("matrice2.in","r",stdin);
    freopen("matrice2.out","w",stdout);
    int n,Q,i,j;
    read(n);
    read(Q);
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            read(a[i][j]);
    while(Q--)
    {
        read(x1);
        read(y1);
        read(x2);
        read(y2);
        BFS();
    }

    return 0;
}