Pagini recente » Cod sursa (job #2839147) | Cod sursa (job #1488247) | Cod sursa (job #1006800) | Cod sursa (job #834210) | Cod sursa (job #1906932)
#include<iostream>
#include<fstream>
#include<cstring>
#include<queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int MapR[100][100];
int MapJ[100][100];
int minim;
int N,M;
int i,j,finiR,finjR,finiJ,finjJ;
int di[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[] = {1, -1, 0, 0, 1, -1, 1, -1};
queue <pair < int, int > > CoadaJ;
queue <pair < int, int > > CoadaR;
bool ok(int x, int y){
if(x<0 || y<0)
return 0;
if(x>=N || y>=M)
return 0;
return 1;
}
void LeeJulieta()
{
int i, j, iUrmator, jUrmator;
while (!CoadaJ.empty())
{
i = CoadaJ.front().first;
j = CoadaJ.front().second;
CoadaJ.pop();
for (int directie = 0; directie < 8; directie++)
{
iUrmator = i + di[directie];
jUrmator = j + dj[directie];
if (ok(iUrmator, jUrmator) && MapJ[iUrmator][jUrmator] == 0)
{
MapJ[iUrmator][jUrmator] = MapJ[i][j] + 1;
CoadaJ.push(make_pair(iUrmator, jUrmator));
}
}
}
}
void LeeRomeo()
{
int i, j, iUrmator, jUrmator;
while (!CoadaR.empty())
{
i = CoadaR.front().first;
j = CoadaR.front().second;
CoadaR.pop();
for (int directie = 0; directie < 8; directie++)
{
iUrmator = i + di[directie];
jUrmator = j + dj[directie];
if (ok(iUrmator, jUrmator) && MapR[iUrmator][jUrmator] == 0)
{
MapR[iUrmator][jUrmator] = MapR[i][j] + 1;
CoadaR.push(make_pair(iUrmator, jUrmator));
}
}
}
}
int main(){
char c;
int ct,ci,cj;
fin>>N>>M;
fin.get(c);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
fin.get(c);
if(c=='X'){
MapR[i][j]=-1;
MapJ[i][j]=-1;
}
else if(c=='R'){
MapR[i][j]=1;
MapJ[i][j]=0;
CoadaR.push(make_pair(i,j));
finiJ= i;
finjJ=j;
}
else if(c=='J'){
MapJ[i][j]=1;
MapR[i][j]=0;
CoadaJ.push(make_pair(i,j));
finiR=i;
finjR=j;
}
else{
MapR[i][j]=0;
MapJ[i][j]=0;
}
}
fin.get(c);
}
LeeJulieta();
LeeRomeo();
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if ((MapR[i][j] == MapJ[i][j]) && MapR[i][j] > 0 && MapR[i][j] < ct)
{
ct = MapR[i][j];
ci = i + 1;
cj = j + 1;
}
}
}
fout << ct << " " << ci << " " << cj;
return 0;
}