Cod sursa(job #2870803)

Utilizator acostin643costin andrei acostin643 Data 12 martie 2022 16:11:48
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <queue>

using namespace std;

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

queue <pair <int, int>> q;


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

int a[1005][1005], foc[1005][1005];
bool paftenie[1005][1005];

int main()
{
    int n, m, rez;
    char c;
    pair<int, int> aux, nou, st, fn;
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            fin >> c;
            if(c == '.')
                a[i][j] = 1;
            if(c == 'I')
            {
                a[i][j] = 2;
                st = make_pair(i, j);
            }
            if(c == 'D')
            {
                a[i][j] = 3;
                q.push(make_pair(i, j));
            }
            if(c == 'O')
            {
                fn = make_pair(i, j);
                a[i][j] = 4;
            }
        }
    }
    while(!q.empty())
    {
        aux = q.front();
        for(int i = 1; i < 4; i++)
        {
            nou = make_pair(aux.first + dx[i], aux.second + dy[i]);
            if((a[nou.first][nou.second] == 1 || a[nou.first][nou.second] == 2) && !foc[nou.first][nou.second])
            {
                q.push(nou);
                foc[nou.first][nou.second] = foc[aux.first][aux.second] + 1;
            }
        }
        q.pop();
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
            fout << foc[i][j] << " ";
        fout << '\n';
    }
    return 0;
}