Cod sursa(job #2981443)

Utilizator AswVwsACamburu Luca AswVwsA Data 17 februarie 2023 22:39:03
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <fstream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long
using namespace std;

const int NMAX = 101;
int v[NMAX + 1][NMAX + 1];
char s[NMAX + 4][NMAX + 4];
int d[NMAX + 1][NMAX + 1][2];
int di[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dj[] = {1, 0, -1, -1, 1, -1, 0, 1};

int n, m;
bool inside(int i, int j)
{
    return i >= 1 and i <= n and j >= 1 and j <= m;
}

void lee(pair <int, int> src, int ok)
{
    d[src.first][src.second][ok] = 1;
    queue <pair <int, int> > q;
    q.push(src);
    while (!q.empty())
    {
        pair <int, int> f = q.front();
        q.pop();
        for (int i = 0; i < 8; i++)
        {
            int ni = f.first + di[i];
            int nj = f.second + dj[i];
            if (inside(ni, nj) and d[ni][nj][ok] == 0 and s[ni][nj] != 'X')
            {
                d[ni][nj][ok] = d[f.first][f.second][ok] + 1;
                q.push({ni, nj});
            }
        }
    }
}
int main()
{
    ifstream cin("rj.in");
    ofstream cout("rj.out");
    int i, j;
    cin >> n >> m;
    cin.get();
    pair <int, int> romeo, jul;
    for (i = 1; i <= n; i++)
    {
        cin.getline(s[i] + 1, NMAX);
        for (j = 1; j <= m; j++)
            if (s[i][j] == 'R')
                romeo = {i, j};
            else if (s[i][j] == 'J')
                jul = {i, j};
    }
    lee(romeo, 0);
    lee(jul, 1);
    /*for (i = 1; i <= n; i++, cout << '\n')
        for (j = 1; j <= m; j++)
            cout << d[i][j][0] << " ";
    cout << "\n";
    for (i = 1; i <= n; i++, cout << '\n')
        for (j = 1; j <= m; j++)
            cout << d[i][j][1] << " ";*/
    int mn = INT_MAX;
    pair <int, int> sol;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
            if (s[i][j] == ' ' and d[i][j][0] != 0 and d[i][j][1] != 0 and
                    d[i][j][0] == d[i][j][1])
            {
                if (mn > d[i][j][0])
                {
                    mn = d[i][j][0];
                    sol = {i, j};
                }
            }
    cout << mn << " " << sol.first << " " << sol.second;
}