Pagini recente » Cod sursa (job #2167826) | Cod sursa (job #1537182) | Cod sursa (job #783910) | Cod sursa (job #1725411) | Cod sursa (job #2699924)
#include <fstream>
#include <queue>
#include <string>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
int mat_romeo[105][105],mat_julieta[105][105],n,m;
struct coordonate
{
int x,y;
};
queue <coordonate> q;
coordonate dir[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
bool verif_mat(coordonate poz)
{
return poz.x>=1 && poz.x<=n && poz.y>=1 && poz.y<=m;
}
void lee(coordonate pers, int mat[][105])
{
mat[pers.x][pers.y]=1;
q.push(pers);
while(!q.empty())
{
coordonate casuta_curenta;
casuta_curenta=q.front();
for(int i=0;i<8;i++)
{
coordonate vecin;
vecin.x=casuta_curenta.x+dir[i].x;
vecin.y=casuta_curenta.y+dir[i].y;
if(verif_mat(vecin)==false || mat[vecin.x][vecin.y]!=0)
{
continue;
}
mat[vecin.x][vecin.y]=mat[casuta_curenta.x][casuta_curenta.y]+1;
q.push(vecin);
}
q.pop();
}
}
void printmat() {
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
out<<mat_julieta[i][j]<<' ';
}
out<<'\n';
}
out<<'\n';}
int main()
{
int rasp=100000000,raspx,raspy;
coordonate romeo,julieta;
string s;
in>>n>>m;
getline(in,s);
for(int i=1;i<=n;i++)
{
getline(in,s);
for(int j=1;j<=m;j++)
{
char simbol;
simbol=s[j-1];
if(simbol=='R')
{
romeo.x=i;
romeo.y=j;
}
else if(simbol=='J')
{
julieta.x=i;
julieta.y=j;
}
else if(simbol=='X')
{
mat_romeo[i][j]=-1;
mat_julieta[i][j]=-1;
}
}
}
lee(romeo, mat_romeo);
lee(julieta, mat_julieta);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mat_julieta[i][j]==mat_romeo[i][j] && mat_julieta[i][j]>0)
{
if(mat_julieta[i][j]<rasp)
{
rasp=mat_julieta[i][j];
raspx=i;
raspy=j;
}
}
}
}
out<<rasp<<' '<<raspx<<' '<<raspy;
}