Pagini recente » Istoria paginii runda/urmasiiluidoru/clasament | Cod sursa (job #2606626) | Cod sursa (job #3175368) | Cod sursa (job #2504938) | Cod sursa (job #2438477)
#include <fstream>
#include <queue>
#define SH short
using namespace std;
ifstream cin ("rj.in");
ofstream cout ("rj.out");
struct Point {
SH x, y;
};
SH n, m;
char ch;
Point R, J;
char mat[105][105];
SH dp[2][105][105];
queue <Point> q;
SH dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
SH dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
void lee(Point st, SH ok) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++)
dp[ok][i][j] = (SH)1e5;
}
for(int i = 0; i <= n + 1; i++)
dp[ok][i][0] = dp[ok][i][m + 1] = (SH)1e5;
for(int i = 0; i <= m + 1; i++)
dp[ok][0][i] = dp[ok][n + 1][i] = (SH)1e5;
q.push(st);
dp[ok][st.x][st.y] = 1;
while(!q.empty()) {
Point p = q.front();
q.pop();
for(int i = 0; i < 8; i++) {
Point newp = {p.x + dx[i], p.y + dy[i]};
if(mat[newp.x][newp.y] != 'X' && dp[ok][newp.x][newp.y] > dp[ok][p.x][p.y] + 1) {
dp[ok][newp.x][newp.y] = dp[ok][p.x][p.y] + 1;
q.push(newp);
}
}
}
}
int main() {
cin >> n >> m >> ws;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin.get(ch);
mat[i][j] = ch;
if(ch == 'R')
R = {i, j};
else if(ch == 'J')
J = {i, j};
}
cin >> ws;
}
lee(R, 0);
lee(J, 1);
int tmn = (SH)1e5, l = 0, c = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(dp[0][i][j] != (SH)1e5 && dp[0][i][j] == dp[1][i][j]) {
if(dp[1][i][j] < tmn) {
tmn = dp[0][i][j];
l = i;
c = j;
}
}
}
}
cout << tmn << " " << l << " " << c;
return 0;
}