Cod sursa(job #1311548)

Utilizator DiClauDan Claudiu DiClau Data 8 ianuarie 2015 12:37:18
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.87 kb
#include<stdio.h>
using namespace std;
const int L = 1001;
const int lin[] = {-1, 0, 1, 0},
          col[] = {0, 1, 0, -1};
struct custom
{
    int x, y;
};
int matD[L][L], max = -1, mat[L][L];
custom coada[L * L];

void leeD(int n, int m, int sfarsit)
{
    int inceput = 0;
    custom poz;
    int i;
    while (inceput != sfarsit)
    {
        for (i = 0; i <= 3; i++)
        {
            poz.x = coada[inceput].x + lin[i];
            poz.y = coada[inceput].y + col[i];
            if (poz.x > 0 && poz.y > 0 && poz.x <= n && poz.y <= m && matD[poz.x][poz.y] == 0)
            {
                coada[sfarsit] = poz;
                sfarsit++;
                matD[poz.x][poz.y] = matD[coada[inceput].x][coada[inceput].y] + 1;
                if (matD[poz.x][poz.y] > max)
                    max = matD[poz.x][poz.y];
            }
        }
        inceput++;
    }
}

bool fill(int n, int m, custom start, custom destinatie, int lim)
{
    custom poz;
    mat[start.x][start.y] = 1;
    int i;
    for (i = 0; i <= 3; i++)
    {
        poz.x = start.x + lin[i];
        poz.y = start.y + col[i];
        if (poz.x > 0 && poz.y > 0 && poz.x <= n && poz.y <= m && mat[poz.x][poz.y] == 0 && matD[poz.x][poz.y] >= lim)
        {
            if (poz.x == destinatie.x && poz.y == destinatie.y)
                return true;
            if(fill(n, m, poz, destinatie, lim)) return true;
        }
    }
    return false;
}

int main ()
{
    FILE *in, *out;
    in = fopen ("barbar.in", "r");
    out = fopen ("barbar.out", "w");
    int n, m;
    fscanf(in, "%d%d", &n, &m);
    char x;
    fscanf(in, "%c", &x);
    int i,j, c = 0;
    custom start, destinatie;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            x = fgetc(in);
            if (x == '*')
            {
                matD[i][j] = -1;
                mat[i][j] = -1;
            }
            if (x == 'D')
            {
                coada[c].x = i;
                coada[c].y = j;
                matD[i][j] = 1;
                c++;
            }
            if (x == 'I')
            {
                start.x = i;
                start.y = j;
            }
            if (x == 'O')
            {
                destinatie.x = i;
                destinatie.y = j;
            }
           // fprintf(out, "%d ", matD[i][j]);
        }
        x = fgetc(in);
        //fprintf(out, "\n");
    }
    leeD(n, m, c);

    int pas = 1;
    while (pas < max)
        pas = pas * 2;
    pas = pas / 2;
    i = 0;
    int i2;

    while (pas != 0)
    {
        if (fill(n, m, start,destinatie,i+pas))
            i += pas;
        for (i2 = 1; i2 <= n; i2++)
                for (j = 1; j <= m; j++)
                    if (mat[i2][j] > 0)
                        mat[i2][j] = 0;

        pas = pas / 2;
    }
    if (i != 0)
        fprintf(out, "%d", i-1);

    if (i == 0 )
        fprintf(out, "-1");
    return 0;
}