Pagini recente » Cod sursa (job #35163) | Cod sursa (job #2050644) | Cod sursa (job #1372281) | Cod sursa (job #1588527) | Cod sursa (job #2987894)
#include <fstream>
#include <string.h>
#include <queue>
#include <math.h>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
const int DIM = 100;
const int LETT = 2;
bool close[DIM][DIM];
int dist[DIM][DIM][LETT];
bool viz[DIM][DIM][LETT];
int n, m;
struct coords {
int x;
int y;
};
bool ins(int x, int y) {
return (x >= 0 && x < n && y > 0 && y < m);
}
int dl[4] = { -1, 0, 1, 0 };
int dc[4] = { 0, -1, 0, 1 };
void bfs(int sx, int sy, int z) {
queue<coords> q;
q.push({sx, sy});
viz[sx][sy][z] = true;
dist[sx][sy][z] = 1;
while(!q.empty()) {
coords coord = q.front();
q.pop();
int x = coord.x;
int y = coord.y;
viz[x][y][z] = true;
for(int i = 0; i < 4; i++) {
int dx = x + dl[i];
int dy = y + dc[i];
if(!ins(dx, dy)) continue;
if(viz[dx][dy][z]) continue;
if(close[dx][dy]) continue;
if(dist[dx][dy][z] < dist[x][y][z] + 1 && dist[dx][dy][z] != 0) continue;
viz[dx][dy][z] = true;
dist[dx][dy][z] = dist[x][y][z] + 1;
q.push({dx, dy});
}
}
}
int main() {
in >> n >> m;
int jx, jy, rx, ry;
char c;
string ln;
in.get();
for(int i = 0; i < n; i++) {
getline(in, ln);
for(int j = 0; j < m; j++) {
c = ln[j];
if(c == ' ') close[i][j] = false;
else if(c == 'X') close[i][j] = true;
if(c == 'R') { rx = i; ry = j; }
if(c == 'J') { jx = i; jy = j; }
}
}
bfs(rx, ry, 0);
bfs(jx, jy, 1);
int summin = 9999999;
int xmin, ymin;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int delta = abs(dist[i][j][0] - dist[i][j][1]);
if(dist[i][j][0] == 0 || dist[i][j][1] == 0) continue;
if(delta <= 1) {
summin = min(dist[i][j][0], dist[i][j][1]) - !delta;
xmin = i + 1;
ymin = j + 1;
}
}
}
out << summin << " " << xmin << " " << ymin;
}