Pagini recente » Cod sursa (job #1943883) | Cod sursa (job #486) | Cod sursa (job #1891837) | Cod sursa (job #519870) | Cod sursa (job #2765405)
#include <fstream>
#include <queue>
using namespace std;
char mat[105][105];
int dist[105][105];
int distr[105][105];
int distj[105][105];
int n, m;
void lee(pair <int, int> start){
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
queue <pair <int, int>> Q;
Q.push(start);
dist[start.first][start.second] = 1;
auto isInside = [&n, &m](const pair <int, int>& cell) -> bool {
return 1 <= cell.first && cell.first <= n && 1 <= cell.second && cell.second <= m;
};
while(!Q.empty()){
auto cell = Q.front();
Q.pop();
for(int direction = 0; direction <= 7; direction ++){
pair <int, int> x = {cell.first + dx[direction], cell.second + dy[direction]};
if(!isInside(x) || mat[x.first][x.second] == 'X'){
continue;
}
else if(dist[x.first][x.second] == 0){
dist[x.first][x.second] = dist[cell.first][cell.second] + 1;
Q.push(x);
}
}
}
}
ifstream cin("rj.in");
ofstream cout("rj.out");
int main()
{
cin >> n >> m;
char ch;
cin.get();
pair <int, int> startr;
pair <int, int> startj;
for(int i = 1; i <= n; i ++){
ch = ' ';
bool ok = 0;
for(int j = 1; j <= m; j ++){
ch = cin.get();
if(ch == '\n'){
ok = 1;
break;
}
mat[i][j] = ch;
if(ch == 'R'){
startr.first = i;
startr.second = j;
}
else if(ch == 'J'){
startj.first = i;
startj.second = j;
}
}
if(ok == 0){
cin.get();
}
}
lee(startr);
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
distr[i][j] = dist[i][j];
// cout << distr[i][j] << " ";
dist[i][j] = 0;
}
// cout << "\n";
}
// cout << "\n";
lee(startj);
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
distj[i][j] = dist[i][j];
// cout << distj[i][j] << " ";
dist[i][j] = 0;
}
// cout << "\n";
}
bool ok = 0;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
if(distr[i][j] == distj[i][j] && distr[i][j] > 0){
cout << distr[i][j] << " " << i << " " << j;
ok = 1;
break;
}
}
if(ok == 1){
break;
}
}
return 0;
}