Pagini recente » Cod sursa (job #1873802) | Cod sursa (job #1925352) | Cod sursa (job #2736831) | Cod sursa (job #2777824) | Cod sursa (job #334763)
Cod sursa(job #334763)
#include<fstream.h>
#include<stdlib.h>
ifstream fin("rj.in");
ofstream fout("rj.out");
int n,m,a[101][101],b[101][101];
int x,y,tmin,xr,yr,xj,yj;
int lin[9]={-1,-1,-1,0,0,1,1,1};
int col[9]={-1,0,1,-1,1,-1,0,1};
void citire()
{int i,j;
char c;
fin>>n>>m;
fin.get();
for(i=1;i<=n;i++)
{for(j=1;j<=m;j++)
{fin.get(c);
if(c==' ') a[i][j]=0;
else if(c=='X') a[i][j]=1;
else if(c=='R') {a[i][j]=2;
xr=i;
yr=j;
}
else {a[i][j]=3;
xj=i;
yj=j;
}
}
fin.get();
}
}
void lee()
{int i,j,pas=1,xx,yy,p;
typedef enum{DA,NU} boolean;
boolean gasit=NU;
b[xr][yr]=pas;
b[xj][yj]=pas;
while(gasit==NU)
{gasit=DA;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(b[i][j]==pas)
for(p=0;p<=7;p++)
{xx=i+lin[p];
yy=j+col[p];
if(xx>=1 && xx<=n && yy>=1 && yy<=m)
if(a[xx][yy]==0 )
if(b[xx][yy]==0)
{b[xx][yy]=pas+1;
gasit=NU;
}
else if(b[xx][yy]==pas+1)
{tmin=pas+1;
x=xx;
y=yy;
fout<<tmin<<" "<<x<<" "<<y;
exit(0);
}
}
pas++;
}
}
int main()
{citire();
lee();
return 0;
}