Pagini recente » Cod sursa (job #1047615) | Cod sursa (job #901347) | Cod sursa (job #2586432) | Cod sursa (job #426227) | Cod sursa (job #2672104)
#include <fstream>
#include <iostream>
#include <queue>
#include <cctype>
#include <string>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int mat[105][105];
int romeo[105][105], julieta[105][105];
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int n, m;
struct Coordonate
{
int lin, col;
};
queue <Coordonate> q;
char a[10005];
bool InBounds(Coordonate coord, int mat[105][105])
{
return 0 < coord.lin && coord.lin <=n && 0 < coord.col && coord.col <=m;
}
bool IsFree(Coordonate coord, int mat[105][105])
{
return mat[coord.lin][coord.col]==0;
}
void lee(int x, int y, int mat[105][105])
{
Coordonate coord;
coord.lin=x;
coord.col=y;
q.push(coord);
while (!q.empty())
{
Coordonate next_coord, current_coord=q.front();
q.pop();
for(int i=0; i<8; i++)
{
next_coord.lin=current_coord.lin+dir[i][0];
next_coord.col=current_coord.col+dir[i][1];
if(InBounds(next_coord, mat) && IsFree(next_coord, mat))
{
mat[next_coord.lin][next_coord.col]=mat[current_coord.lin][current_coord.col] + 1;
q.push(next_coord);
}
}
}
}
int main()
{
int i,j;
int xr, yr, xj, yj;
int valmin, imin, jmin;
char a[105];
fin>>n>>m>>ws;
for(i=0; i<n; i++)
{
fin.getline(a, 100);
for(j=0; j<m; j++)
{
if(a[j]=='R')
{
romeo[i+1][j+1]=1;
xr=i+1;
yr=j+1;
}
if(a[j]=='J')
{
julieta[i+1][j+1]=1;
xj=i+1;
yj=j+1;
}
if(a[j]=='X')
{
julieta[i+1][j+1]=-1;
romeo[i+1][j+1]=-1;
}
}
}
lee(xr, yr, romeo);
lee(xj, yj, julieta);
valmin=julieta[xr][yr];
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(romeo[i][j]<1)
continue;
if(romeo[i][j]==julieta[i][j])
{
if(romeo[i][j]<valmin)
{
valmin=romeo[i][j];
imin=i;
jmin=j;
}
if(romeo[i][j]==valmin)
{
if(i<imin)
{
valmin=romeo[i][j];
imin=i;
jmin=j;
}
if(i==imin && j<jmin)
{
valmin=romeo[i][j];
imin=i;
jmin=j;
}
}
}
}
}
fout<<valmin<<' '<<imin<<' '<<jmin;
}