Cod sursa(job #331309)

Utilizator bog29Antohi Bogdan bog29 Data 13 iulie 2009 17:21:17
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include<fstream>
#define dmax 102
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
short int n,m,xr,yr,xj,yj,temp[dmax][dmax],temp2[dmax][dmax];
char ln[dmax];
bool mat[dmax][dmax];
const short int dx[]={9,1,0,-1,-1,-1,0,1,1};
const short int dy[]={9,-1,-1,-1,0,1,1,1,0,};
struct queue
{	short int x;
	short int y;
	short int pas;
}	c[50*dmax];
bool bune(short int i,short int j)
{	return ((i>0)&&(i<=n)&&(j>0)&&(j<=m));
}
void lee1()
{	short int k,p1=1,p2=1,ii,jj,ok=0;
	c[1].x=xr;
	c[1].y=yr;
	c[1].pas=1;
	temp[xr][yr]=1;
	while(p1<=p2)
	{	for(k=1;k<=8;k++)
		{	ii=c[p1].x+dx[k];
			jj=c[p1].y+dy[k];
			if(bune(ii,jj))
			{	if((temp[ii][jj]==0)&&(mat[ii][jj]==0))
				{	p2++;
					c[p2].x=ii;
					c[p2].y=jj;
					c[p2].pas=c[p1].pas;
					temp[ii][jj]=temp[c[p1].x][c[p1].y]+1;
				}
			}
		}
		p1++;	
	}
}	
void lee2()
{	short int k,p1=1,p2=1,ii,jj,ok=0;
	c[1].x=xj;
	c[1].y=yj;
	c[1].pas=1;
	temp2[xj][yj]=1;
	while(p1<=p2)
	{	for(k=1;(k<=8)&&(ok==0);k++)
		{	ii=c[p1].x+dx[k];
			jj=c[p1].y+dy[k];
			if(bune(ii,jj))
			{	if((temp2[ii][jj]==0)&&(mat[ii][jj]==0))
				{	p2++;
					c[p2].x=ii;
					c[p2].y=jj;
					temp2[ii][jj]=temp2[c[p1].x][c[p1].y]+1;
					if(temp[ii][jj]==temp2[ii][jj])
					{	out<<temp[ii][jj]<<" "<<ii<<" "<<jj;
						ok=1;
					}	
				}
			}
		}
		p1++;	
	}
}	
int main()
{	short int i,j;
	in>>n>>m;
	for(i=1;i<=n+1;i++)
	{	in.getline(ln,m+2,'\n');
		for(j=0;j<m;j++)
		{	if(ln[j]=='X')
				mat[i-1][j+1]=1;
			if(ln[j]=='R')
			{	xr=i-1;
				yr=j+1;
			}
			if(ln[j]=='J')
			{	xj=i-1;
				yj=j+1;
			}
		}	
	}	
	in.close();
	lee1();
	lee2();
	out.close();
	return 0;
}