Cod sursa(job #1142687)

Utilizator ciuschiDragos-Constantin Biciusca ciuschi Data 14 martie 2014 04:00:36
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
struct XoY
{
	int x,y;
}R,J,REZ;
int M1[101][101],M2[101][101],tmin,n,m; //M1-romeo, M2-julieta
int test(int x,int y)
{
	if(x>=1&&y>=1&&x<=n&&y<=m)
		return 1;
	return 0;
}
void bf(int M[101][101],int x,int y)
{
	queue <int> ox,oy;
	ox.push(x);
	oy.push(y);
	M[x][y]=1;
	while(!ox.empty())
	{	x=ox.front();
		y=oy.front();
		int cc=M[x][y];
		if(test(x-1,y-1)&&M[x-1][y-1]==0)
		{
			M[x-1][y-1]=cc+1;
			ox.push(x-1);
			oy.push(y-1);
		}
		if(test(x-1,y)&&M[x-1][y]==0)
		{
			M[x-1][y]=cc+1;
			ox.push(x-1);
			oy.push(y);
		}
		if(test(x-1,y+1)&&M[x-1][y+1]==0)
		{
			M[x-1][y+1]=cc+1;
			ox.push(x-1);
			oy.push(y+1);
		}
		if(test(x,y-1)&&M[x][y-1]==0)
		{
			M[x][y-1]=cc+1;
			ox.push(x);
			oy.push(y-1);
		}
		if(test(x,y+1)&&M[x][y+1]==0)
		{
			M[x][y+1]=cc+1;
			ox.push(x);
			oy.push(y+1);
		}
		if(test(x+1,y-1)&&M[x+1][y-1]==0)
		{
			M[x+1][y-1]=cc+1;
			ox.push(x+1);
			oy.push(y-1);
		}
		if(test(x+1,y)&&M[x+1][y]==0)
		{
			M[x+1][y]=cc+1;
			ox.push(x+1);
			oy.push(y);
		}
		if(test(x+1,y+1)&&M[x+1][y+1]==0)
		{
			M[x+1][y+1]=cc+1;
			ox.push(x+1);
			oy.push(y+1);
		}
		ox.pop();
		oy.pop();
	}
}
int main()
{
	f>>n>>m;
	char c;
	f.get(c);
	for(int i=1;i<=n;i++)
	{	for(int j=1;j<=m;j++)
		{
			f.get(c);
			if(c=='X')M1[i][j]=M2[i][j]=-1;
			else if(c=='R'){R.x=i;R.y=j;}
			else if(c=='J'){J.x=i;J.y=j;}
			else if(c=='\n')break;
		}
	if(c!='\n')f.get(c);
	}
	f.close();
	bf(M1,R.x,R.y);
	bf(M2,J.x,J.y);
	tmin=99999;
	for(int i=1;i<=n;i++)
	{	for(int j=1;j<=m;j++)
		{
			cout<<M2[i][j]<<" ";
			if(M1[i][j]!=-1&&M1[i][j]!=0&&M1[i][j]==M2[i][j]&&M1[i][j]<tmin)
			{
				REZ.x=i;
				REZ.y=j;
				tmin=M1[i][j];
			}
		}
	cout<<'\n';
	}
	g<<tmin<<" "<<REZ.x<<" "<<REZ.y;
}