Cod sursa(job #1337943)

Utilizator western100Sutu Eusebiu western100 Data 9 februarie 2015 17:39:15
Problema Kdrum Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <fstream>
#include <list>
#include <queue>

using namespace std;

ifstream f("kdrum.in");
ofstream g("kdrum.out");

short n,m,k,x1,y1,x2,y2;
int a[52][52];
bool viz[52][52];
short fl[52][52];

struct coord
{
    short x,y;
};

list <coord> d;

void citire()
{
    f>>n>>m>>k;
    f>>x1>>y1>>x2>>y2;
    short i,j;
    int x;
    if(k!=1)
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                f>>x;
                if(x)
                    {
                        a[i][j]=x%k;
                        if(!a[i][j])
                            d.push_back({i,j});
                    }
                else
                    a[i][j]=-1;
            }
    else
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                f>>x;
                if(x)
                    a[i][j]=1;
                else
                    a[i][j]=0;
            }

}

short maxi(short a,short b,short c,short d)
{
    short x=a;
    if(x<b)
        x=b;
    if(x<c)
        x=c;
    if(x<d)
        x=d;
    return x;
}

void rez1()
{
    short i,j;
    queue < coord > cd;
    cd.push({x1,y1});
    viz[x1][y1]=true;
    fl[x1][y1]=1;
    while(cd.size())
    {
        i=cd.front().x;
        j=cd.front().y;
        if(a[i][j+1] and !viz[i][j+1])
        {
            fl[i][j+1]=maxi(fl[i][j],fl[i][j+2],fl[i-1][j+1],fl[i+1][j+1])+1;
            viz[i][j+1]=true;
            cd.push({i,j+1});
        }
        if(a[i][j-1] and !viz[i][j-1])
        {
            fl[i][j-1]=maxi(fl[i][j],fl[i][j-2],fl[i-1][j-1],fl[i+1][j-1])+1;
            viz[i][j-1]=true;
            cd.push({i,j-1});
        }
        if(a[i+1][j] and !viz[i+1][j])
        {
            fl[i+1][j]=maxi(fl[i][j],fl[i+2][j],fl[i+1][j+1],fl[i+1][j-1])+1;
            viz[i+1][j]=true;
            cd.push({i+1,j});
        }
        if(a[i-1][j] and !viz[i-1][j])
        {
            fl[i-1][j]=maxi(fl[i][j],fl[i-2][j],fl[i-1][j+1],fl[i-1][j-1])+1;
            viz[i-1][j]=true;
            cd.push({i-1,j});
        }
        cd.pop();
    }
    g<<fl[x2][y2];
}

void rezolvare()
{
    if(k==1)
        rez1();
    else
    {

    }
}

int main()
{
    citire();
    rezolvare();
    return 0;
}