Pagini recente » Cod sursa (job #2975746) | Cod sursa (job #406651) | Cod sursa (job #2036356) | Cod sursa (job #2551731) | Cod sursa (job #2426482)
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
const int nmax = 105;
ifstream fin("rj.in");
ofstream fout("rj.out");
queue < pair < int, int > > C;
int di[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int dj[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int mR[nmax][nmax], mJ[nmax][nmax], n, m;
char s[nmax];
struct coord
{
int i, j;
}R, J;
void read()
{
fin >> n >> m;
unsigned int j = 0;
fin.get();
for(int i = 1; i <= n; ++i)
{
fin.get(s, nmax);
for(j = 0; j < strlen(s); ++j)
{
if(s[j] == 'R')
{
R.i = i;
R.j = j + 1;
}
if(s[j] == 'J')
{
J.i = i;
J.j = j + 1;
}
if(s[j] == 'X')
{
mR[i][j + 1] = -1;
mJ[i][j + 1] = -1;
}
}
fin.get();
}
}
bool ok(int i, int j, int matrice[nmax][nmax])
{
return i >= 1 && j >= 1 && i <= n && j <= m && matrice[i][j] == 0;
}
void Lee(int x, int y, int matrice[nmax][nmax])
{
matrice[x][y] = 1;
C.push(make_pair(x, y));
int i, j;
while(!C.empty())
{
i = C.front().first;
j = C.front().second;
C.pop();
for(int d = 0; d < 8; d++)
{
int urmatorul_i = i + di[d];
int urmatorul_j = j + dj[d];
if(ok(urmatorul_i, urmatorul_j, matrice))
{
C.push(make_pair(urmatorul_i, urmatorul_j));
matrice[urmatorul_i][urmatorul_j] = 1 + matrice[i][j];
}
}
}
}
int main()
{
read();
Lee(R.i, R.j, mR);
Lee(J.i, J.j, mJ);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
if(mR[i][j] == mJ[i][j] && mR[i][j] > 0 && mJ[i][j] > 0)
{
fout << mR[i][j] << " " << i << " " << j << "\n";
break;
break;
}
}
return 0;
}