Pagini recente » Cod sursa (job #726766) | Cod sursa (job #2315935) | Cod sursa (job #3354294) | Cod sursa (job #1312477) | Cod sursa (job #3320368)
#include <iostream>
#include <queue>
#include <fstream>
#include <string>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int romeo[102][102]={0};
int julieta[102][102];
string s;
int n, m;
int dl[]={0, 0, 1, 1, 1, -1, -1, -1};
int dc[]={-1, 1, 1, -1, 0, -1, 1, 0};
queue < pair < int, int > >q;
bool verificare(int x, int y)
{
return ((x < n && x>=0)&&(y>=0 && y <m));
}
void lee(int x2, int y2, int a[102][102])
{
a[x2][y2]=1;
q.push({x2, y2});
int cnt=1;
while(!q.empty())
{
x2 = q.front().first;
y2 = q.front().second;
q.pop();
for(int dr = 0; dr < 8; dr++ )
{
int nextl = x2 + dl[dr];
int nextc = y2 + dc[dr];
if(verificare(nextl, nextc))
{
if(a[nextl][nextc]==0)
{
a[nextl][nextc]=a[x2][y2]+1;
q.push({nextl,nextc});
}
}
}
}
}
int main()
{
fin >> n >> m;
int xr, yr, xj, yj;
getline(fin, s);
for(int i = 0; i < n; i++)
{
getline(fin,s);
for(int j = 0; j < m; j++)
{
if(s[j] == 'X')
{
romeo[i][j]=-1;
julieta[i][j]=-1;
}
if(s[j] == 'R')
{
romeo[i][j]=0;
julieta[i][j]=-1;
xr=i;
yr=j;
}
if(s[j] == 'J')
{
romeo[i][j]=-2;
julieta[i][j]=0;
xj=i;
yj=j;
}
}
}
lee(xr, yr, romeo);
lee(xj, yj, julieta);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if((romeo[i][j] > 0) && (romeo[i][j] == julieta[i][j]))
{
fout << romeo[i][j] << ' '<< i+1 << ' ' << j+1;
break;
}
}
}
return 0;
}