Cod sursa(job #2457471)

Utilizator butnaru_vlad2003Butnaru Vlad butnaru_vlad2003 Data 17 septembrie 2019 21:12:55
Problema Kdrum Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream in ("kdrum.in");
ofstream out ("kdrum.out");
short dx[4] = {0,1,0,-1};
short dy[4] = {1,0,-1,0};
struct info
{
    int dist,x,y,tatax,tatay,rez;
};
queue <info> q;
int a[51][51];
int n,m,k,x1,y1,x2,y2;
bool verf (int x,int y)
{
    if(x>n || x<1 || y>m || y<1 ||a[x][y]==0)
        return 0;
    return 1;
}
void lee (int startx, int starty)
{
    info start;
    start.dist = a[startx][starty];
    start.x=startx;
    start.y=starty;
    start.tatax = -1;
    start.tatay = -1;
    start.rez = 1;
    q.push(start);
    while (!q.empty())
    {
        info pct;
        pct=q.front();
        for (int i = 0;i<=3;++i)
        {
            int newx = pct.x+dx[i];
            int newy = pct.y+dy[i];
            if (verf(newx,newy) && !(pct.tatax==newx && pct.tatay==newy))
            {
                int newdist = ((pct.dist%k) * (a[newx][newy] %k))%k;
                if (newdist == 0 && newx==x2 && newy==y2)
                {
                    out<<pct.rez+1;
                    return;
                }
                else
                    q.push({newdist,newx,newy,newx,newy,pct.rez+1});
            }
        }
        q.pop();
    }
    return ;
}
int main ()
{
    in>>n>>m>>k>>x1>>y1>>x2>>y2;
    for (int i = 1;i<=n;++i)
        for(int j = 1;j<=m;++j)
            in>>a[i][j];
    lee(x1,y1);
}