Pagini recente » Cod sursa (job #2789611) | Cod sursa (job #2973726) | Cod sursa (job #295187) | Cod sursa (job #1442621) | Cod sursa (job #2162616)
#include <fstream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN = 1e2;
const int DIR = 8;
const int INF = 0x3f3f3f3f;
char mat[MAXN + 2][MAXN + 2];
int rom[MAXN + 2][MAXN + 2], jul[MAXN + 2][MAXN + 2];
int dirl[] = {-1, -1, 0, 1, 1, 1, 0, -1}, dirc[] = {0, 1, 1, 1, 0, -1, -1, -1};
queue <pair <int, int>> q;
inline void bfs(int lin, int col, int marc[MAXN + 2][MAXN + 2]) {
memset(marc, INF, sizeof(marc[0][0]) * (MAXN + 2) * (MAXN + 2));
marc[lin][col] = 1;
q.emplace(lin, col);
while (q.empty() == false) {
lin = q.front().first;
col = q.front().second;
q.pop();
for (int d = 0; d < DIR; ++d) {
int newl = lin + dirl[d], newc = col + dirc[d];
if (mat[newl][newc] != 'X' && marc[newl][newc] == INF) {
marc[newl][newc] = marc[lin][col] + 1;
q.emplace(newl, newc);
}
}
}
}
int main()
{
int n, m, l, c, rl, rc, jl, jc;
memset(mat, 'X', sizeof mat);
ifstream fin("rj.in");
fin >> n >> m;
fin.get();
for (int i = 1; i <= n; ++i) {
fin.getline(mat[i] + 1, MAXN);
for (int j = 1; j <= m; ++j)
if (mat[i][j] == 'R') {
rl = i; rc = j;
mat[i][j] = ' ';
} else if (mat[i][j] == 'J') {
jl = i; jc = j;
mat[i][j] = ' ';
}
}
fin.close();
bfs(rl, rc, rom);
bfs(jl, jc, jul);
l = c = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (rom[i][j] == jul[i][j] && rom[i][j] < rom[l][c]) {
l = i; c = j;
}
ofstream fout("rj.out");
fout << rom[l][c] << " " << l << " " << c;
fout.close();
return 0;
}