Cod sursa(job #3341815)

Utilizator AndreiTifuiTifui Andrei AndreiTifui Data 21 februarie 2026 11:31:46
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
int n,m;
int di[]={-1,-1,-1,0,1,1,1,0};
int dj[]={-1,0,1,1,1,0,-1,-1};
int b[101][101],b2[101][101],a1[101][101];
bool test(int i,int j)
{
    return i<=n && j<=m && i>=1 && j>=1;
}
void lee(int i,int j,int c[101][101])
{
    queue<pair<int,int>> q;
    q.push(make_pair(i,j));
    while(!q.empty())
    {
        pair<int,int> ax;
        ax=q.front();
        i=ax.first;
        j=ax.second;
        q.pop();
        for(int k=0;k<8;++k)
        {
            int ii=i+di[k];
            int jj=j+dj[k];
            if(a1[ii][jj] &&c[ii][jj]>c[i][j]+1)
            {
                c[ii][jj]=c[i][j]+1;
                q.push(make_pair(ii,jj));
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(NULL);
    in.tie(NULL);
    in>>n;
    in>>m;
    in.get();
    for(int i=1;i<=n;++i)
        {
            char x;
            a1[i][0]=a1[i][m+1]=0;
        for(int j=1;j<=m;++j)
    {
        a1[0][j]=a1[n+1][j]=0;
            in.get(x);
            if(x=='J')
                a1[i][j]=2;
            else
                if(x=='R')
                    a1[i][j]=3;
                else
                    if(x=='X')
                        a1[i][j]=0;
                    else
                    a1[i][j]=1;
            if(a1[i][j])
                b[i][j]=b2[i][j]=100000;
    }
            in.get(x);
        }
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
           if(a1[i][j]==2)
              {
                  b[i][j]=1;
                lee(i,j,b);
              }
            if(a1[i][j]==3)
                {
                b2[i][j]=1;
                lee(i,j,b2);
                }

        }
    int mini=100000000,minix,miniy;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            if(b[i][j]==b2[i][j] && b[i][j])
                if(b[i][j]<mini)
                {
                    mini=b[i][j];
                    minix=i;
                    miniy=j;
                }
    out<<mini<<' '<<minix<<' '<<miniy;
    return 0;
}