Cod sursa(job #1816299)

Utilizator teo.cons98Constantin Teodor-Claudiu teo.cons98 Data 26 noiembrie 2016 12:40:21
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.64 kb
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

ifstream fin("barbar.in");
ofstream fout("barbar.out");

char A[1001][1001];
int n, m, pozix, poziy, pozBuna = -1;
int B[1001][1001];
int distDrag[1001][1001], px[1000001], py[1000001];

void citire()
{
    fin>>n>>m;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
        {
            fin>>A[i][j];
            if(A[i][j] == 'I')
            {
                pozix = i;
                poziy = j;
            }
        }
}

void golB()
{
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            B[i][j] = 0;
        }
    }
}

int verificare(int distMin)
{
    golB();
    int  p = 1, u = 1, x, y;
    px[1] = pozix;
    py[1] = poziy;
    B[px[1]][py[1]] = 1;
    while(p <= u)
    {
        x = px[p];
        y = py[p];
        if(A[x - 1][y] == '.' and distDrag[x - 1][y] >= distMin and B[x - 1][y] == 0 and x - 1 >= 1)
        {
            ++u;
            px[u] = x - 1;
            py[u] = y;
            B[x - 1][y] = 1;
        }
        if(A[x - 1][y] == 'O' and distDrag[x - 1][y] >= distMin and x - 1 >= 1)
        {
            return 1;
            p = u + 10;
            break;
        }
        if(A[x + 1][y] == '.' and distDrag[x + 1][y] >= distMin and B[x + 1][y] == 0 and x + 1 <= n)
        {
            ++u;
            px[u] = x + 1;
            py[u] = y;
            B[x + 1][y] = 1;
        }
        if(A[x + 1][y] == 'O' and distDrag[x + 1][y] >= distMin and x + 1 <= n)
        {
            return 1;
            p = u + 10;
            break;
        }
        if(A[x][y - 1] == '.' and distDrag[x][y - 1] >= distMin and B[x][y - 1] == 0 and y - 1 >= 1)
        {
            ++u;
            px[u] = x;
            py[u] = y - 1;
            B[x][y-1]= 1;
        }
        if(A[x][y - 1] == 'O' and distDrag[x][y - 1] >= distMin and y - 1 >= 1)
        {
            return 1;
            p = u + 10;
            break;
        }
        if(A[x][y + 1] == '.' and distDrag[x][y + 1] >= distMin and B[x][y + 1] == 0 and y + 1 <= m)
        {
            ++u;
            px[u] = x;
            py[u] = y + 1;
            B[x][y+1] = 1;
        }
        if(A[x][y + 1] == 'O' and distDrag[x][y + 1] >= distMin and y + 1 <= m)
        {
            return 1;
            p = u + 10;
            break;
        }
        ++p;
    }
    return 0;
}

void cautBin(int prim, int ult)
{
    if(prim <= ult)
    {
        int mid = (prim + ult) / 2;
        if(distDrag[pozix][poziy] >= mid)
        {
            if(verificare(mid))
            {
                pozBuna = mid;
                cautBin(mid + 1, ult);
            }
            else
            {
                cautBin(prim, mid - 1);
            }
        }
        else
        {
            cautBin(prim, mid - 1);
        }
    }

}

void calcDistDrag()
{
    int p = 1, u = 0, x, y;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            if(A[i][j] == 'D')
            {
                ++u;
                px[u] = i;
                py[u] = j;
            }
        }
    }
    cout<<p<<" "<<u<<endl;
    while(p <= u)
    {
        x = px[p];
        y = py[p];
        if((A[x - 1][y] == '.' or A[x - 1][y] == 'I' or A[x - 1][y] == 'O') and distDrag[x - 1][y] == 0 and x - 1 >= 1)
        {
            ++u;
            px[u] = x - 1;
            py[u] = y;
            distDrag[x - 1][y] = distDrag[x][y] + 1;
        }
        if((A[x + 1][y] == '.' or A[x + 1][y] == 'I' or A[x + 1][y] == 'O') and distDrag[x + 1][y] == 0 and x + 1 <= n)
        {
            ++u;
            px[u] = x + 1;
            py[u] = y;
            distDrag[x + 1][y] = distDrag[x][y] + 1;
        }
        if((A[x][y - 1] == '.' or A[x][y - 1] == 'I' or A[x][y - 1] == 'O') and distDrag[x][y - 1] == 0 and y - 1 >= 1)
        {
            ++u;
            px[u] = x;
            py[u] = y - 1;
            distDrag[x][y - 1] = distDrag[x][y] + 1;
        }
        if((A[x][y + 1] == '.' or A[x][y + 1] == 'I' or A[x][y + 1] == 'O') and distDrag[x][y + 1] == 0 and y + 1 <= m)
        {
            ++u;
            px[u] = x;
            py[u] = y + 1;
            distDrag[x][y + 1] = distDrag[x][y] + 1;
        }
        ++p;
    }
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            cout<<distDrag[i][j]<<" ";
        }
        cout<<endl;
    }
}

int main()
{
    citire();
    calcDistDrag();
    cautBin(0,1000);
    fout<<pozBuna;
}