Cod sursa(job #408900)

Utilizator StromsWolfie Fjordstrom Stroms Data 3 martie 2010 12:30:30
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <fstream>
#include <iostream>
using namespace std;

int plaque[1000][1000], Rx, Ry, Jx, Jy, n, m, over, under, half_life;
struct tailmate {int lin, col;} core[1000], vixen;
int mx[4]={-1, 0, 1, 0};
int my[4]={0, 1, 0, -1};

void read()
{ifstream f("rj.in");
f>>n>>m;
char convo;
f.get();
for (int i=1; i<=n; i++)
	{for (int j=1; j<=m; j++)
		{f.get(convo);
		if (convo==' ') plaque[i][j]=0;
		else
			if (convo=='X') plaque[i][j]=-1;
			else
				if (convo=='R')
					{plaque[i][j]=1;
					Rx=i;
					Ry=j;
					}
				else
					{Jx=i;
					Jy=j;
					}
		}
	f.get();
	}
f.close();
}



int checkmate(int x, int y)
{return x>=1 && x<=n && y>=1 && y<=m && plaque[x][y]==0;}

void addman(int x, int y)
{over++;
core[over].lin=x;
core[over].col=y;
}

int yiff(int x, int y)
{return plaque[x][y]==under-1;}


int main()
{read();
under=1;
addman(Rx, Ry);
while (under<=over)
	{for (int i=0; i<4; i++)
		if (checkmate(core[under].lin+mx[i], core[under].col+my[i]))
			{plaque[core[under].lin+mx[i]][core[under].col+my[i]]=plaque[core[under].lin][core[under].col]+1;
			addman(core[under].lin+mx[i], core[under].col+my[i]);
			}
	under++;
	}

under=plaque[Jx][Jy];
half_life=under/2;
vixen.lin=Jx;
vixen.col=Jy;
while (under>1)
	{for (int i=0; i<4; i++)
		if (yiff(vixen.lin+mx[i], vixen.col+my[i]))
		{under--;
		vixen.lin=vixen.lin+mx[i];
		vixen.col=vixen.col+my[i];
		break;
		}
	if (plaque[vixen.lin][vixen.col]==half_life)
		{break;}
	}
ofstream f("rj.out");
f<<vixen.lin<<" "<<vixen.col<<" "<<half_life;
f.close();		
return 0;
}