Cod sursa(job #77736)

Utilizator megabyteBarsan Paul megabyte Data 14 august 2007 18:59:10
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <stdio.h>
#define NMAX 128
#define INF "rj.in"
#define OUF "rj.out"
int n,m,r[NMAX][NMAX]={0};
char h[NMAX][NMAX];
FILE *out;
struct coord
{
  int x,y;
};
void wayform(int mat[][NMAX],int l,int c)
{
  int i,j,ok=1,k=1,w=0,t,r,pas=2,lm[8]={-1,-1,-1,0,0,1,1,1},cm[8]={-1,0,1,-1,1,-1,0,1};
  coord s[4*NMAX],d[4*NMAX],*a,*b,*q;
  mat[l][c]=1;
  s[0].x=l;s[0].y=c;a=s;b=d;
  while(ok)
  {
    ok=0;
    for(t=0;t<k;t++)
    {
      q=a+t;
      for(r=0;r<8;r++)
      {
	i=q->x+lm[r];
	j=q->y+cm[r];
	if(i>=0&&j>=0&&i<n&&j<m)
	{
	  if(h[i][j]==' '&&mat[i][j]==0)
	  {
	    mat[i][j]=pas;
	    (b+w)->x=i;
	    (b+w)->y=j;
	    w++;ok=1;
	  }
	}
      }
    }
    q=a;a=b;b=q;
    k=w;w=0;pas++;
  }
}
/*void post(int mat[][NMAX])
{
  int i,j;
  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++) fprintf(out,"%d ",mat[i][j]);
    fprintf(out,"\n");
  }
  fprintf(out,"\n");
} */
int main()
{
  int j[NMAX][NMAX]={0},min=10000,l,c;
    coord mn,ro,ju;
  FILE *in;
  in=fopen(INF,"r");
  out=fopen(OUF,"w");
    if(!in) fprintf(out,"0 ");
  else{
  fscanf(in,"%d%d",&n,&m);
  for(l=0;l<n;l++)
   {
   for(c=0;c<m;c++)
   {
      h[l][c]=fgetc(in);
      if(h[l][c]=='\n')       h[l][c]=fgetc(in);
    //  fputc(h[l][c],out);
      if(h[l][c]=='R') {ro.x=l;ro.y=c;}
      else if(h[l][c]=='J') {ju.x=l;ju.y=c;}
   }
   //fputc('\n',out);
   }
  wayform(r,ro.x,ro.y);
  wayform(j,ju.x,ju.y);
  //post(r);
 // post(j);
  for(l=0;l<n;l++)
   for(c=0;c<m;c++)
    if(h[l][c]!='X')
      if(r[l][c]==j[l][c]&&r[l][c]<min&&r[l][c]!=0){
					 min=r[l][c];
					 mn.x=l+1;
					 mn.y=c+1;
					}
   fprintf(out,"%d %d %d",min,mn.x,mn.y);
   }
  fclose(in);fclose(out);
  return 0;
}