Cod sursa(job #1155897)

Utilizator donydony2009FMI - Donisan George donydony2009 Data 27 martie 2014 11:41:53
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.09 kb
#include<fstream>
#define NV 10001
#define N 10002
#define NE 10003
#define E 10004
#define SE 10005
#define S 10006
#define SV 10007
#define V 10008
#define START 10009
using namespace std;
struct coords{
	int x,y;
}Q1[1000],J,r,RESULT;
int m,n,len;
int A[101][101];
int first1,last1;
void push1(int x,int y)
{
	if(last1==999)
		last1=-1;
	Q1[++last1].x=x;
	Q1[last1].y=y;
}
void pop1(void)
{
	if(first1==999)
		first1=0;
	else
		first1++;
}
coords peek1(void)
{
	return Q1[first1];
}
void citire(void)
{
	char P[105];
	int i,j;
	ifstream f("rj.in");
	f>>m>>n;
	f.get();
	for(i=1;i<=m;i++)
	{
		
		f.getline(P,101,'\n');
		for(j=0;j<n;j++)
		{
			if(P[j]==' ')
				A[i][j+1]=1;
			if(P[j]=='X')
				A[i][j+1]=0;
			if(P[j]=='R')
			{
				r.x=i;
				r.y=j+1;
			}
			if(P[j]=='J')
			{
				J.x=i;
				J.y=j+1;
			}
		}
	}
}
void Min(void)
{
	int i;
	coords c;
	push1(r.x,r.y);
	A[r.x][r.y]=3;
	A[J.x][J.y]=2;
	while(1)
	{
		c=peek1();
		//NV
		if(c.x>1&&c.y>1&&A[c.x-1][c.y-1]==1)
		{
			push1(c.x-1,c.y-1);
			A[c.x-1][c.y-1]=NV;
		}
		if(c.x>1&&c.y>1&&A[c.x-1][c.y-1]==2)
		{
			A[c.x-1][c.y-1]=NV;
			break;
		}
		//N
		if(c.x>1&&A[c.x-1][c.y]==1)
		{
			push1(c.x-1,c.y);
			A[c.x-1][c.y]=N;
		}
		if(c.x>1&&A[c.x-1][c.y]==2)
		{
			A[c.x-1][c.y]=N;
			break;
		}
		//NE
		if(c.x>1&&c.y<n&&A[c.x-1][c.y+1]==1)
		{
			push1(c.x-1,c.y+1);
			A[c.x-1][c.y+1]=NE;
		}
		if(c.x>1&&c.y<n&&A[c.x-1][c.y+1]==2)
		{
			A[c.x-1][c.y+1]=NE;
			break;
		}
		//V
		if(c.y>1&&A[c.x][c.y-1]==1)
		{
			push1(c.x,c.y-1);
			A[c.x][c.y-1]=V;
		}
		if(c.y>1&&A[c.x][c.y-1]==2)
		{
			A[c.x][c.y-1]=V;
			break;
		}
		//E
		if(c.y<n&&A[c.x][c.y+1]==1)
		{
			push1(c.x,c.y+1);
			A[c.x][c.y+1]=E;
		}
		if(c.y<n&&A[c.x][c.y+1]==2)
		{
			A[c.x][c.y+1]=E;
			break;
		}
		//SV
		if(c.x<m&&c.y>1&&A[c.x+1][c.y-1]==1)
		{
			push1(c.x+1,c.y-1);
			A[c.x+1][c.y-1]=SV;
		}
		if(c.x<m&&c.y>1&&A[c.x+1][c.y-1]==2)
		{
			A[c.x+1][c.y-1]=SV;
			break;
		}
		//S
		if(c.x<m&&A[c.x+1][c.y]==1)
		{
			push1(c.x+1,c.y);
			A[c.x+1][c.y]=S;
		}
		if(c.x<m&&A[c.x+1][c.y]==2)
		{
			A[c.x+1][c.y]=S;
			break;
		}
		//SE
		if(c.x<m&&c.y<n&&A[c.x+1][c.y+1]==1)
		{
			push1(c.x+1,c.y+1);
			A[c.x+1][c.y+1]=SE;
		}
		if(c.x<m&&c.y<n&&A[c.x+1][c.y+1]==2)
		{
			A[c.x+1][c.y+1]=SE;
			break;
		}
		pop1();
	}
}
int drumLen(int x,int y)
{
	if(A[x][y]==3)
		return len;
	if(A[x][y]==N)
	{
		len++;
		return drumLen(x+1,y);
	}
	if(A[x][y]==NV)
	{
		len++;
		return drumLen(x+1,y+1);
	}
	if(A[x][y]==NE)
	{
		len++;
		return drumLen(x+1,y-1);
	}
	if(A[x][y]==V)
	{
		len++;
		return drumLen(x,y+1);
	}
	if(A[x][y]==E)
	{
		len++;
		return drumLen(x,y-1);
	}
	if(A[x][y]==SV)
	{
		len++;
		return drumLen(x-1,y+1);
	}
	if(A[x][y]==S)
	{
		len++;
		return drumLen(x-1,y);
	}
	if(A[x][y]==SE)
	{
		len++;
		return drumLen(x-1,y-1);
	}
}
void Cpoint(int x,int y,int check,int LENGHT)
{
	
	if(LENGHT==len)
	{
		if(check==0)
		{
			RESULT.x=x;
			RESULT.y=y;
		}
		else
		{
			if(A[x][y]==S||A[x][y]==SE||A[x][y]==SV||A[x][y]==E)
				Cpoint(x,y,0,LENGHT+1);
			else
			{
				RESULT.x=x;
				RESULT.y=y;
			}
		}
	}
	if(A[x][y]==N)
	{
		len++;
		Cpoint(x+1,y,check,LENGHT);
	}
	if(A[x][y]==NV)
	{
		len++;
		Cpoint(x+1,y+1,check,LENGHT);
	}
	if(A[x][y]==NE)
	{
		len++;
		Cpoint(x+1,y-1,check,LENGHT);
	}
	if(A[x][y]==V)
	{
		len++;
		Cpoint(x,y+1,check,LENGHT);
	}
	if(A[x][y]==E)
	{
		len++;
		Cpoint(x,y-1,check,LENGHT);
	}
	if(A[x][y]==SV)
	{
		len++;
		Cpoint(x-1,y+1,check,LENGHT);
	}
	if(A[x][y]==S)
	{
		len++;
		Cpoint(x-1,y,check,LENGHT);
	}
	if(A[x][y]==SE)
	{
		len++;
		Cpoint(x-1,y-1,check,LENGHT);
	}
}
int main(void)
{
	ofstream g("rj.out");
	int Total;
	len=1;
	citire();
	Min();
	
	coords c;
	Total=drumLen(J.x,J.y);
	len=1;
	if(Total%2==1)
	{
		Cpoint(J.x,J.y,0,Total/2+1);
		g<<Total/2+1<<" "<<RESULT.x<<" "<<RESULT.y;
	}
	else
	{
		Cpoint(J.x,J.y,1,Total/2);
		g<<Total/2<<" "<<RESULT.x<<" "<<RESULT.y;
	}
	
}