Cod sursa(job #254463)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 7 februarie 2009 12:18:21
Problema Kdrum Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 1.56 kb
#include <stdio.h>
#include <set>
#include <vector>

#define maxn 53
#define maxk 1203
#define mp make_pair

using namespace std;

char f[maxn][maxn][maxk];
long v[maxn][maxn];
pair <long, pair<long, long> > c1[100010], c2[100010];
long n, m, i, j, k, x1, y1, x2, y2, l1, l2, x, y, rest, xc, yc, rc, ok, sol;
const long d1[]={0, 1, 0, -1};
const long d2[]={1, 0, -1, 0};

int main()
{
    freopen("kdrum.in", "r", stdin);
    freopen("kdrum.out", "w", stdout);
    scanf("%d %d %d", &n, &m, &k);
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            scanf("%d ", &v[i][j]);
        }
    }
    rest=v[x1][y1]%k;
    f[x1][y1][rest]=1;
    l1=1;
    c1[1]=mp(rest, mp(x1, y1) );
    ok=1;
    sol=1;
    if(x1==x2 && y1==y2 && rest==0) ok=0;
    while(l1 && ok)
    {
        sol++;
        l2=0;
        for(i=1; i<=l1; i++)
        {
            rc=c1[i].first;
            xc=c1[i].second.first;
            yc=c1[i].second.second;
            for(j=0; j<4; j++)
            {
                x=xc+d1[j];
                y=yc+d2[j];
                rest=(rc*v[x][y])%k;
                if(f[x][y][rest]==0 && v[x][y]!=0)
                {
                    l2++;
                    c2[l2]=mp(rest, mp(x, y));
                    if(x==x2 && y==y2 && rest==0) ok=0;
                }
            }
        }
        l1=l2;
        for(i=1; i<=l1; i++)
        {
            c1[i]=c2[i];
        }
    }
    printf("%d\n", sol);
    return 0;
}