Cod sursa(job #2498055)

Utilizator LeCapataIustinian Serban LeCapata Data 23 noiembrie 2019 14:07:03
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4 kb
1:48 PM
#include <fstream>
#include <queue>
using namespace std;

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

int mat[1005][1005];
int r, c, nr_drag;

int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};

pair<int, int> coord_drag[1005], coord_start, coord_final;
queue<pair<int,int> > coada;

void captusire()
{
    for(int i=0; i<=r+1; i++)
        mat[i][0]=mat[i][c+1]=-1;

    for(int j=0; j<=c+1; j++)
        mat[0][j]=mat[r+1][j]=-1;
}

void alg_Lee(int x, int y)
{
    mat[x][y]=0;
    coada.push(make_pair(x,y));
    while(!coada.empty())
    {
        int okx=coada.front().first;
        int oky=coada.front().second;
        coada.pop();

        for(int i=0; i<4; i++)
        {
            int x2=okx+dx[i];
            int y2=oky+dy[i];

            if(mat[x2][y2]>mat[okx][oky]+1 || mat[x2][y2]==0)
            {
                mat[x2][y2]=mat[okx][oky]+1;
                coada.push(make_pair(x2,y2));
            }
        }
    }
}

int main()
{
    in>>r>>c;
    captusire();

    for(int i=1; i<=r; i++)
        for(int j=1; j<=c; j++)
        {
            char x;
            in>>x;

            if(x=='.')
                mat[i][j]=0;
            else if(x=='*')
                mat[i][j]=-1;
            else if(x=='D')
            {
                mat[i][j]=0;
                coord_drag[++nr_drag]=make_pair(i,j);
            }
            else if(x=='I')
            {
                mat[i][j]=0;
                coord_start=make_pair(i,j);
            }
            else if(x=='O')
            {
                mat[i][j]=0;
                coord_final=make_pair(i,j);
            }
        }

    for(int i=1; i<=nr_drag; i++)
        alg_Lee(coord_drag[i].first,coord_drag[i].second);

    for(int i=1; i<=r; i++)
    {
        for(int j=1; j<=c; j++)
            out<<mat[i][j]<<"  ";
        out<<'\n';
    }

    in.close();
    out.close();
    return 0;
}

2:06 PM
#include <fstream>
#include <queue>
using namespace std;

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

int mat[1005][1005];
int r, c, nr_drag;

int dx[] = { 0,0,1,-1 };
int dy[] = { -1,1,0,0 };

pair<int, int> coord_drag[1005], coord_start, coord_final;
queue<pair<int, int> > coada;

void captusire()
{
    for (int i = 0; i <= r + 1; i++)
        mat[i][0] = mat[i][c + 1] = -1;


    for (int j = 0; j <= c + 1; j++)
        mat[0][j] = mat[r + 1][j] = -1;
}

void alg_Lee()
{
    while (!coada.empty())
    {
        int okx = coada.front().first;
        int oky = coada.front().second;
        coada.pop();


        for (int i = 0; i < 4; i++)
        {
            int x2 = okx + dx[i];
            int y2 = oky + dy[i];


            if (mat[x2][y2] > mat[okx][oky] + 1 || mat[x2][y2] == 0)
            {
                mat[x2][y2] = mat[okx][oky] + 1;
                coada.push(make_pair(x2, y2));
            }
        }
    }
}


int main()
{
    in >> r >> c;
    captusire();

    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++)
        {
            char x;
            in >> x;

            if (x == '.')
                mat[i][j] = 0;
            else if (x == '*')
                mat[i][j] = -1;
            else if (x == 'D')
            {
                mat[i][j] = 0;
                coord_drag[++nr_drag] = make_pair(i, j);
            }
            else if (x == 'I')
            {
                mat[i][j] = 0;
                coord_start = make_pair(i, j);
            }
            else if (x == 'O')
            {
                mat[i][j] = 0;
                coord_final = make_pair(i, j);
            }
        }


    for (int i = 1; i <= nr_drag; i++)
    {
        coada.push(make_pair(coord_drag[i].first, coord_drag[i].second));
    }

    alg_Lee();

    for (int i = 1; i <= nr_drag; i++)
    {
        mat[coord_drag[i].first][coord_drag[i].second] = 0;
    }

    out<<-1;
    in.close();
    out.close();
    return 0;
}