Cod sursa(job #3272675)

Utilizator lucaclgluca clg lucaclg Data 30 ianuarie 2025 18:24:38
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3 kb
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
char character[101];
int i, j, n, m, a[101][101], x_start, y_start, x2_start, y2_start, b[101][101];
int dx[] = {-1, -1, 0, 1, 1,  1,  0, -1};
int dy[] = { 0,  1, 1, 1, 0, -1, -1, -1};
queue <pair<int,int>> q;
vector<pair<int,int>> drumr;
vector<pair<int,int>> drumj;
bool in_matrix(int x, int y)
{
    if(x >= 1 && y >= 1 && x <= n && y <=  m)
        return true;
    return false;
}
void lee(int a[101][101], int x_start, int y_start, int x_finish, int y_finish)
{
    q.push({x_start, y_start});
    a[x_start][y_start] = 1;
    while(!q.empty())
    {
        int x_curent = q.front().first;
        int y_curent = q.front().second;
        for(i = 0; i <= 7; i++)
        {
            int x_vecin = x_curent + dx[i];
            int y_vecin = y_curent + dy[i];
            if(a[x_vecin][y_vecin] == 0 && in_matrix(x_vecin, y_vecin) == true)
            {
                q.push({x_vecin, y_vecin});
                a[x_vecin][y_vecin] = a[x_curent][y_curent] + 1;
            }
            /*else if(x_start == x_finish && y_start == y_finish)
            {
                a[x_vecin][y_vecin] = a[x_curent][y_curent] + 1;
                while(!q.empty())
                    q.pop();
            }
            */
        }
        q.pop();

    }
    while(!q.empty())
        q.pop();
}
int main()
{
    fin >> n >> m;
    fin.get();
    for(i = 1; i <= n; i++){
        fin.get(character, 101);
        fin.get();
        int nr = strlen(character);
        for(j = 0; j < nr; j++)
        {
            if(character[j] == 'X')
                a[i][j + 1] = -1;
            else if(character[j] == ' ')
                a[i][j + 1] = 0;
            else if(character[j] == 'J'){
                x2_start = i;
                y2_start = j + 1;
            }
            else if(character[j] == 'R')
            {
                x_start = i;
                y_start = j + 1;
            }
        }
    }
    for(i = 1; i <= n; i++)
        for(j = 1; j <= m; j++)
            b[i][j] = a[i][j];
    lee(a, x_start, y_start, x2_start, y2_start);
    lee(b, x2_start, y2_start, x_start, y_start);
    /*
    for(i = 1; i <= n; i++){
        for(j = 1; j <= m; j++)
            fout << a[i][j] << ' ';
        fout << '\n';
    }
    fout << '\n';
    for(i = 1; i <= n; i++){
        for(j = 1; j <= m; j++)
            fout << b[i][j] << ' ';
        fout << '\n';
        }
    */
    for(i = 1; i <= n; i++)
        for(j = 1; j <= m; j++)
            if(a[i][j] == b[i][j] && a[i][j] != 0 && a[i][j] != -1)
            {
                fout << a[i][j] << ' ' << i << ' ' << j;
                return 0;
            }

}
/// -1, nu pot merge
///  0, pot merge
///  -2, start, il consider pe Romeo start
///  -3, finish
/*
5 5
R XX
X X X
X XXX
X X X
X J X
*/
///se vor intalni peste nr de pasi a lui romeo / 2 + 1 (5 / 2 + 1)