Pagini recente » Cod sursa (job #689431) | Cod sursa (job #696997) | Cod sursa (job #2544472) | Cod sursa (job #1346781) | Cod sursa (job #2638038)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ifstream fin("rj.in");
ofstream fout("rj.out");
const int di[8] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dj[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int mt[105][105], R[105][105], J[105][105];
queue<pair<int, int>> q;
pair<int, int> cor;
int tmin = INT_MAX;
int n, m;
inline bool ok(int i, int j)
{
return i && j && i <= n && j <= m && !mt[i][j];
}
void Lee(int sx, int sy, int li[105][105])
{
q.push({sx, sy});
li[sx][sy] = 1;
while (!q.empty())
{
int i, j;
i = q.front().first;
j = q.front().second;
q.pop();
for (int k = 0; k < 8; ++k)
{
int ni, nj;
ni = di[k] + i;
nj = dj[k] + j;
if (ok(ni, nj) && li[ni][nj] == 0)
{
q.push({ni, nj});
li[ni][nj] = li[i][j] + 1;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
fin.tie(0);
pair<int, int> pozr, pozj;
string lin;
fin >> n >> m;
fin.get();
for (int i = 1; i <= n; ++i)
{
getline(fin, lin);
int j = 1;
for (auto c : lin)
{
if (c == 'X') mt[i][j] = 1;
else if (c == 'R') pozr = {i, j};
else if (c == 'J') pozj = {i, j};
else mt[i][j] = 0;
++j;
}
}
Lee(pozr.first, pozr.second, R);
Lee(pozj.first, pozj.second, J);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (R[i][j] && R[i][j] == J[i][j] && R[i][j] < tmin)
tmin = R[i][j], cor.first = i, cor.second = j;
fout << tmin << " " << cor.first << " " << cor.second;
return 0;
}