Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
2
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

long long int n, m, l, c, v[105][105], ans, minnr;
queue <pair<int, int > > q;

int main()
{
    ifstream fin("schior.in");
    ofstream fout("schior.out");

    fin>>n>>m;
    fin>>l>>c;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            fin>>v[i][j];
        }
    }
    minnr=v[l][c];

    q.push({l, c});

    if(l-1>=1 && v[l-1][c]<=minnr)
    {
        q.push({l-1, c});
    }
    if(c-1>=1 && v[l][c-1]<=minnr)
    {
        q.push({l, c-1});
    }
    if(l+1<=n && v[l+1][c]<=minnr)
    {
        q.push({l+1, c});
    }
    if(c+1<=m && v[l][c+1]<=minnr)
    {
        q.push({l, c+1});
    }
    if(l-1>=1 && c-1>=1 && v[l-1][c-1]<=minnr)
    {
        q.push({l-1, c-1});
    }
    if(l+1<=n && c+1<=m && v[l+1][c+1]<=minnr)
    {
        q.push({l+1, c+1});
    }
    if(l-1>=1 && c+1<=m && v[l-1][c+1]<=minnr)
    {
        q.push({l-1, c+1});
    }
    if(c-1>=1 && l+1<=n && v[l+1][c-1]<=minnr)
    {
        q.push({l+1, c-1});
    }

    while(!q.empty())
    {

        pair<int, int> p=q.front();

        q.pop();

        if(v[p.first][p.second]<=minnr)
        {
            minnr=v[p.first][p.second];
            l=p.first;
            c=p.second;
            if(l-1>=1 && v[l-1][c]<=minnr)
            {
                q.push({l-1, c});
            }
            if(c-1>=1 && v[l][c-1]<=minnr)
            {
                q.push({l, c-1});
            }
            if(l+1<=n && v[l+1][c]<=minnr)
            {
                q.push({l+1, c});
            }
            if(c+1<=m && v[l][c+1]<=minnr)
            {
                q.push({l, c+1});
            }
            if(l-1>=1 && c-1>=1 && v[l-1][c-1]<=minnr)
            {
                q.push({l-1, c-1});
            }
            if(l+1<=n && c+1<=m && v[l+1][c+1]<=minnr)
            {
                q.push({l+1, c+1});
            }
            if(l-1>=1 && c+1<=m && v[l-1][c+1]<=minnr)
            {
                q.push({l-1, c+1});
            }
            if(c-1>=1 && l+1<=n && v[l+1][c-1]<=minnr)
            {
                q.push({l+1, c-1});
            }
        }

    }

    fout<<minnr;

    return 0;
}