Cod sursa(job #1083979)

Utilizator adrianvVasiu Adrian-Daniel adrianv Data 16 ianuarie 2014 16:33:27
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.36 kb
#include<queue>
#include<utility>
#include <stdio.h>
#include<vector>
using namespace std;
FILE *in, *out;

#define x first
#define y second

vector<pair<int,int> >v;
queue<pair<int,int> >coada;
pair<int,int>initial;
pair<int,int>obstacol;
pair<int,int>destin;
int mat[120][120],i,j,n,m;
char car;
int main()
{
	in = fopen("rj.in", "rt");
	out = fopen("rj.out", "wt");

	fscanf(in,"%d",&n);
	fscanf(in,"%d",&m);
	fscanf(in,"%c",&car);

	for(i=1;i<=n;i++)
			mat[i][0]=mat[i][m+1]=-1;
	for(i=1;i<=m;i++)
			mat[0][i]=mat[n+1][i]=-1;


	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			fscanf(in,"%c",&car);
			if(car==' ')
				mat[i][j]=-2;
			else if(car=='X')
				mat[i][j]=-1;
			else if(car=='R')
				{
					mat[i][j]=0;
					initial.x=i;
					initial.y=j;
				}
			else if(car=='J')
			{
				mat[i][j]=0;
				destin.x=i;
				destin.y=j;
			}
		}
		fscanf(in,"%c",&car);
	}
	

	coada.push(initial);
	coada.push(destin);

		while(!coada.empty() )
		{
			pair<int,int>curent=coada.front();
			coada.pop();

			if(mat[curent.x-1][curent.y-1]==-2 || mat[curent.x-1][curent.y-1]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x-1][curent.y-1]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x-1][curent.y-1]+1,curent.x-1,curent.y-1);
					break;
				}
				else
					{
						mat[curent.x-1][curent.y-1]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x-1,curent.y-1));
					}
			}
			if(mat[curent.x-1][curent.y+1]==-2 || mat[curent.x-1][curent.y+1]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x-1][curent.y+1]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x-1][curent.y+1]+1,curent.x-1,curent.y+1);
					break;
				}
				else
					{
						mat[curent.x-1][curent.y+1]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x-1,curent.y+1));
					}
			}
			if(mat[curent.x+1][curent.y-1]==-2 || mat[curent.x+1][curent.y-1]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x+1][curent.y-1]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x+1][curent.y-1]+1,curent.x+1,curent.y-1);
					break;
				}
				else
					{
						mat[curent.x+1][curent.y-1]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x+1,curent.y-1));
					}
			}
			if(mat[curent.x+1][curent.y+1]==-2 || mat[curent.x+1][curent.y+1]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x+1][curent.y+1]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x+1][curent.y+1]+1,curent.x+1,curent.y+1);
					break;
				}
				else
					{
						mat[curent.x+1][curent.y+1]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x+1,curent.y+1));
					}
			}
			if(mat[curent.x+1][curent.y]==-2 || mat[curent.x+1][curent.y]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x+1][curent.y]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x+1][curent.y]+1,curent.x+1,curent.y);
					break;
				}
				else
					{
						mat[curent.x+1][curent.y]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x+1,curent.y));
					}
			}
			if(mat[curent.x-1][curent.y]==-2 || mat[curent.x-1][curent.y]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x-1][curent.y]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x-1][curent.y]+1,curent.x-1,curent.y);
					break;
				}
				else
					{
						mat[curent.x-1][curent.y]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x-1,curent.y));
					}
			}
			if(mat[curent.x][curent.y+1]==-2 || mat[curent.x][curent.y+1]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x][curent.y+1]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x][curent.y+1]+1,curent.x,curent.y+1);
					break;
				}
				else
					{
						mat[curent.x][curent.y+1]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x,curent.y+1));
					}
			}
			if(mat[curent.x][curent.y-1]==-2 || mat[curent.x][curent.y-1]==mat[curent.x][curent.y]+1)
			{
				if(mat[curent.x][curent.y-1]==mat[curent.x][curent.y]+1)
				{
					fprintf(out,"%d %d %d",mat[curent.x][curent.y-1]+1,curent.x,curent.y-1);
					break;
				}
				else
					{
						mat[curent.x][curent.y-1]=mat[curent.x][curent.y]+1;
						coada.push(make_pair(curent.x,curent.y-1));
					}
			}
			


		}




	fclose(in);
	fclose(out);
	return 0;
}