Pagini recente » Cod sursa (job #3176799) | Cod sursa (job #131392) | Cod sursa (job #1517280) | Cod sursa (job #906844) | Cod sursa (job #3290343)
#include <fstream>
#include <queue>
#define inf 100000000
using namespace std;
ifstream cin("rj.in");
ofstream cout("rj.out");
char v[105];
pair<int,int>r,jul,rez;
int dist1[105][105];
int dist2[105][105];
void lee(pair<int,int> a,int dist[][105])
{
dist[a.first][a.second]=1;
queue<pair<int,int>>q;
q.push(a);
while(!q.empty())
{
int i=q.front().first,j=q.front().second;
q.pop();
//cout<<i<<" "<<j<<'\n';
for(int i1=-1; i1<=1; i1++)
{
for(int j1=-1; j1<=1; j1++)
{
if(dist[i+i1][j+j1]==0)
{
dist[i+i1][j+j1]=1+dist[i][j];
q.push({i+i1,j+j1});
}
}
}
}
}
int main()
{
int n,m;
cin>>n>>m;
cin.getline(v,105);
for(int i=1; i<=n; i++)
{
cin.getline(v,105);
for(int j=0; j<m; j++)
{
if(v[j]=='X')dist1[i][j+1]=dist2[i][j+1]=inf;
else if(v[j]=='R')r= {i,j+1};
else if(v[j]=='J')jul= {i,j+1};
}
}
for(int i=0; i<=n+1; i++)
dist1[i][0]=dist1[i][m+1]=dist2[i][0]=dist2[i][m+1]=inf;
for(int j=0; j<=m+1; j++)
dist1[0][j]=dist1[n+1][j]=dist2[0][j]=dist2[n+1][j]=inf;
//cout<<r.first<<" "<<r.second<<'\n'<<jul.first<<" "<<jul.second<<'\n';
lee(r,dist1);//cout<<'\n';
lee(jul,dist2);
int min1=inf;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(dist1[i][j]>0 && dist1[i][j]==dist2[i][j] && dist1[i][j]<min1/2)
{
min1=dist1[i][j]+dist2[i][j];
rez={i,j};
}
}
//cout<<'\n';
}
cout<<min1-3<<" "<<rez.first<<" "<<rez.second;
return 0;
}