Pagini recente » Cod sursa (job #1466241) | Cod sursa (job #3245013) | Cod sursa (job #3210997) | Cod sursa (job #2501022) | Cod sursa (job #2660122)
#include <iostream>
#include <queue>
#include <fstream>
using namespace std;
char matr[101][101];
int n, m;
int dirx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int diry[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int matAuxR[101][101];
int matAuxJ[101][101];
void Calculate(int x, int y, int matAux[101][101]){
queue<pair<int, int>> q;
q.push(make_pair(x, y));
matAux[x][y] = 1;
while(!q.empty()){
pair<int, int> currentNode = q.front();
q.pop();
for(int i=0; i<8; i++){
int newPosx = currentNode.first + dirx[i];
int newPosy = currentNode.second + diry[i];
if(newPosx >= 1 && newPosx <= n && newPosy >= 1 && newPosy <= m)
if(matr[newPosx][newPosy] != 'X' && matAux[newPosx][newPosy] == 0){
matAux[newPosx][newPosy] = matAux[currentNode.first][currentNode.second] + 1;
q.push(make_pair(newPosx, newPosy));
}
}
}
}
void Read(){
ifstream fin("rj.in");
fin >> n >> m;
fin.get();
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
fin.get(matr[i][j]);
fin.get();
}
}
int main()
{
ofstream fout("rj.out");
Read();
for(int i=1; i <= n; i++)
for(int j=1; j <= m; j++)
if(matr[i][j] == 'R')
Calculate(i, j, matAuxR);
else if(matr[i][j] == 'J')
Calculate(i, j, matAuxJ);
int posX, posY, minimum = n*m + 1;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(matAuxJ[i][j] == matAuxR[i][j] && matAuxJ[i][j] < minimum && matAuxJ[i][j] != 0){
posX = i;
posY = j;
minimum = matAuxJ[i][j];
}
fout << minimum << " " << posX << " " << posY;
return 0;
}