Cod sursa(job #87173)

Utilizator RobytzzaIonescu Robert Marius Robytzza Data 26 septembrie 2007 19:50:08
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include<fstream.h>
ifstream fin("rj.in");
ofstream fout("rj.out");
int a[100][100],x[10000],y[10000],num=0,x1,y1,col,lin;
const short vx[9]={1,1,1,0,0,-1,-1,-1};
const short vy[9]={1,0,-1,-1,1,-1,0,1};
int n,m,xx,yy;
void citire(){
fin>>n>>m;
char x='0';
for (short i=1;i<=n;i++)   {
   for (short j=1;j<=m;j++){
       fin.get(x);
       if (x=='X')
	  a[i][j]=1;
	  else
	    if (x==' ')
	      a[i][j]=0;
	      else
	       if (x=='R'){
		  a[i][j]=2;
		  xx=i;
		  yy=j;}
		  else
		    if (x=='J') {
		       a[i][j]=0;
		       x1=i;
		       y1=j;}
		      }
fin.get(x);
}
if (xx==1)
yy--;
if (x1==1)
   y1--;
for (short u=2;u<=m+1;u++)
    a[1][u-1]=a[1][u];
fin.close();
}
void marcare(){
for (int i=0;i<=m;i++){
a[0][i]=1;
a[n+1][i]=1;}
for (int j=0;j<=n+1;j++){
a[j][0]=1;
a[j][m+1]=1;}}
int fac(){
for (short i=0;i<8;i++)
    if (a[xx+vx[i]][yy+vy[i]]==0){
       a[xx+vx[i]][yy+vy[i]]=3;
       x[num++]=xx+vx[i];
       y[num-1]=yy+vy[i]; }
for (int j=0;j<num;j++){
  for (short nu=0;nu<8;nu++)
      if (a[x[j]+vx[nu]][y[j]+vy[nu]]==0){
	  a[x[j]+vx[nu]][y[j]+vy[nu]]=a[x[j]][y[j]]+1;
	  x[num++]=x[j]+vx[nu];
	  y[num-1]=y[j]+vy[nu];}
if (a[x1][y1]!=0)
   return a[x1][y1]/2;
}
return 0;
}
void min(){
int minx=1000,maxx=1000;
for (int i=0;i<num,a[x[i]][y[i]]<(a[x1][y1]/2+2);i++)
    if (a[x[i]][y[i]]==a[x1][y1]/2+1)
       if (x[i]<minx){
	  minx=x[i];
	  maxx=y[i];}
       else
	 if (x[i]==minx)
	    if (maxx>y[i]){
	      minx=x[i];
	      maxx=y[i];}
lin=maxx;
col=minx;
}
int main(){
citire();
marcare();
fout<<fac()<<" ";
min();
fout<<col<<" "<<lin<<"\n";
fout.close();
return 0;
}