Pagini recente » Borderou de evaluare (job #805280) | Cod sursa (job #1654364) | Cod sursa (job #1857791) | Cod sursa (job #217729) | Cod sursa (job #1083889)
#include<queue>
#include<utility>
#include <stdio.h>
using namespace std;
FILE *in, *out;
#define x first
#define y second
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]==-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],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],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],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],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;
}