Cod sursa(job #221594)

Utilizator iepureAndreea iepure Data 16 noiembrie 2008 21:52:30
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include<stdio.h>
#include<values.h>
int dx[]={0, -1, -1, 0, 1, 1, 1, 0, -1}, dy[]={0, 0, 1, 1, 1, 0, -1, -1, -1};
int cx[1003], cy[1003], a[101][101], b[101][101];
int main()
{int m, n, i, j, p, u, rx, ry, jx, jy, xn, yn, x, y;
char d;
FILE *f=fopen("rj.in","r");
FILE *g=fopen ("rj.out","w");
fscanf (f, "%d %d", &n, &m);
for (i=1;i<=n;i++) for (j=1;j<=m;j++) {fscanf (f, "%c", &d);
				       if (d=='x') a[i][j]=-1;
				       else if (d==' ') a[i][j]=0;
				       else if (d=='R') {rx=i;
							 ry=j;
							 }
				       else {jx=i;
					     jy=j;
					     }
				       b[i][j]=a[i][j];
				       }
fscanf (f, "%c", &d);

p=u=1;
a[rx][ry]=1;
cx[u]=rx;
cy[u]=ry;
while (p<=u) {x=cx[p];
	      y=cy[p];
	      for (i=1;i<=8;i++) {xn=x+dx[i];
				  yn=y+dy[i];
				  if (a[xn][yn]==0) {a[xn][yn]=a[x][y]+1;
				                     cx[++u]=xn;
						     cy[u]=yn;
                                                     }
				  }
	      p++;
	      }

p=u=1;
b[jx][jy]=1;
cx[u]=jx;
cy[u]=jy;
while (p<=u) {x=cx[p];
	      y=cy[p];
	      for (i=1;i<=8;i++) {xn=x+dx[i];
				  yn=x+dy[i];
				  if (b[xn][yn]==0) {b[xn][yn]=b[x][y]+1;
						     cx[++u]=xn;
						     cy[u]=yn;
						     }
				  }
	      p++;
	      }

int min=MAXINT;
for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (a[i][j]==b[i][j]&&a[i][j]<min) {min=a[i][j];
									  x=i;
									  y=j;
									  }
fprintf(g, "%d %d %d", min, x, y);
fclose(f);
fclose(g);
return 0;
}