Cod sursa(job #2765390)

Utilizator Andrei_TudorAndrei Tudor Andrei_Tudor Data 26 iulie 2021 18:15:54
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <fstream>
#include <queue>
using namespace std;
char mat[105][105];
int dist[105][105];
int distr[105][105];
int distj[105][105];

int n, m;

void lee(pair <int, int> start){
    const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
    const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
    queue <pair <int, int>> Q;
    Q.push(start);
    dist[start.first][start.second] = 1;
    auto isInside = [&n, &m](const pair <int, int>& cell) -> bool {
        return 1 <= cell.first && cell.first <= n && 1 <= cell.second && cell.second <= m;
    };
    while(!Q.empty()){
        auto cell = Q.front();
        Q.pop();
        for(int direction = 0; direction <= 7; direction ++){
            pair <int, int> x = {cell.first + dx[direction], cell.second + dy[direction]};
            if(!isInside(x) || mat[x.first][x.second] == 'X'){
                continue;
            }
            else if(dist[x.first][x.second] == 0){
                dist[x.first][x.second] = dist[cell.first][cell.second] + 1;
                Q.push(x);
            }
        }
    }
}

ifstream cin("rj.in");
ofstream cout("rj.out");
int main()
{
    cin >> n >> m;
    cin.get();
    pair <int, int> startr;
    pair <int, int> startj;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            char ch = cin.get();
            mat[i][j] = ch;
            if(ch == 'R'){
                startr.first = i;
                startr.second = j;
            }
            else if(ch == 'J'){
                startj.first = i;
                startj.second = j;
            }
        }
        cin.get();
    }
    lee(startr);
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            distr[i][j] = dist[i][j];
            dist[i][j] = 0;
        }
    }
    lee(startj);
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            distj[i][j] = dist[i][j];
            dist[i][j] = 0;
        }
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            if(distr[i][j] == distj[i][j] && distr[i][j] > 0){
                cout << distr[i][j] << " " << i << " " << j;
            }
        }
    }
    return 0;
}