Cod sursa(job #2989651)

Utilizator NutaAlexandruASN49K NutaAlexandru Data 6 martie 2023 21:03:05
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <fstream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#include <cassert>
using namespace std;
ifstream cin("padure.in");
ofstream cout("padure.out");
struct cord
{
    int y,x;
    bool in(int n,int m)
    {
        return (y>=0 && y<n && x>=0 && x<m);
    }
    cord operator +(cord b)
    {
        return {y+b.y,x+b.x};
    }
};
vector<cord>dir={
    {1,0},
    {-1,0},
    {0,1},
    {0,-1}
};
void solve()
{
    int n,m;
    cin>>n>>m;
    cord g,h;
    cin>>g.y>>g.x>>h.y>>h.x;
    g.y--;
    g.x--;
    h.x--;
    h.y--;
    vector<vector<int>>a(n,vector<int>(m)),rez(n,vector<int>(m,2e9));
    for(auto &c:a)
    {
        for(auto &v:c)
        {
            cin>>v;
        }
    }
    deque<cord>q;
    q.push_back(g);
    rez[g.y][g.x]=0;
    while(q.size())
    {
        auto then=q.front();
        q.pop_front();
        for(auto &c:dir)
        {
            auto acm=then+c;
            if(acm.in(n,m) && rez[then.y][then.x]+(a[then.y][then.x]!=a[acm.y][acm.x])<rez[acm.y][acm.x])
            {
                rez[acm.y][acm.x]=rez[then.y][then.x]+(a[then.y][then.x]!=a[acm.y][acm.x]);
                if(a[then.y][then.x]!=a[acm.y][acm.x])
                {
                    q.push_back(acm);
                }
                else
                {
                    q.push_front(acm);
                }
            }
        }
    }
    cout<<rez[h.y][h.x]<<' ';

}
main()
{
    auto sol=[](bool x)->string
    {
        if(x)return "Yes";
        return "No";
    };
    int tt=1;

    //cin>>tt;
    while(tt--)
    {
        solve();
    }
}