Pagini recente » Cod sursa (job #2837216) | Cod sursa (job #3226501) | Cod sursa (job #350591) | Cod sursa (job #975824) | Cod sursa (job #901516)
Cod sursa(job #901516)
#include<fstream>
using namespace std;
ifstream f("rj.in");ofstream g("rj.out");
int dx[8]={-1,-1,-1,0,0,1,1,1},dy[8]={-1,0,1,-1,1,-1,0,1};
struct co{ int l,c;} q[10001];
int ar[101][101],aj[101][101],x,xx,y,yy,i,j,xr,yr,xj,yj,p,u;
char A, a[101][101];
int main(){
int n,m;
f>>n>>m; f.get();
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
f.get(A);
if(A=='R'){
a[i][j]=0;
xr=i;
yr=j;}
else
if(A=='J'){
a[i][j]=0;
xj=i;
yj=j;}
else
if(A==' ')
a[i][j]=0;
else
a[i][j]=1;}
f.get();}
for(i=0;i<=m+1;i++)
a[0][i]=a[n+1][i]=1;
for(j=0;j<=n+1;j++)
a[j][0]=a[j][m+1]=1;
//coada pentru romeo
p=u=1;
q[p].l=xr;q[p].c=yr;
ar[xr][yr]=1;
while(p<=u){
x=q[p].l;
y=q[p].c;
for(i=0;i<=7;i++)
if(a[x+dx[i]][y+dy[i]]==0 && ar[x+dx[i]][y+dy[i]]==0)
{
u++;
q[u].l=x+dx[i];
q[u].c=y+dy[i];
ar[x+dx[i]][y+dy[i]]=ar[x][y]+1;
}
p++;
}
p=u=1;
q[p].l=xj;q[p].c=yj;
aj[xj][yj]=1;
while(p<=u){
x=q[p].l;
y=q[p].c;
for(i=0;i<=7;i++)
if(a[x+dx[i]][y+dy[i]]==0 && aj[x+dx[i]][y+dy[i]]==0)
{
u++;
q[u].l=x+dx[i];
q[u].c=y+dy[i];
aj[x+dx[i]][y+dy[i]]=aj[x][y]+1;
}
p++;
}
int min=10001;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(ar[i][j]!=0&&aj[i][j]!=0&&ar[i][j]==aj[i][j])
if(min>aj[i][j]){
min=aj[i][j];
xx=i;
yy=j;
}
g<<min<<" "<<xx<<" "<<yy;
return 0;
}