Pagini recente » Cod sursa (job #1142313) | Cod sursa (job #1279530) | Cod sursa (job #3126631) | Cod sursa (job #2624799) | Cod sursa (job #690052)
Cod sursa(job #690052)
#include<iostream>
#include<fstream>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
struct coada{ int i, j;} c[1000];
int n, m, a[100][100],b[100][100]; //a=romeo, b=julieta
int main()
{
int i, j, pri,prj,pji,pjj,nr, st, sf;
char k[1000];
nr=7;
int diri[]={-1, -1, -1, 0, 1, 1, 1, 0};// NV N NE E SE S SV V
int dirj[]={-1, 0, 1, 1, 1, 0, -1, -1};
f>>n>>m;
f.get();
for(i=0;i<n;i++)
{
f.get(k,1000);
f.get();
for(j=0;j<m;j++)
{
if(k[j]=='X')
a[i][j]=b[i][j]=-1;
else
if(k[j]==' ')
a[i][j]=b[i][j]=2000;
else
if(k[j]=='R')
a[i][j]=1, b[i][j]=2000, pri=i, prj=j;
else
if(k[j]=='J')
b[i][j]=1, a[i][j]=2000, pji=i, pjj=j;
}
}
st=sf=1; //start, sfarsit
c[st].i=pri;
c[st].j=prj;
while(st<=sf)
{
for(i=0;i<8;i++)
if(a[c[st].i][c[st].j]+1<a[c[st].i+diri[i]][c[st].j+dirj[i]])
{
sf++;
a[c[st].i+diri[i]][c[st].j+dirj[i]]=a[c[st].i][c[st].j]+1;
c[sf].i=c[st].i+diri[i];
c[sf].j=c[st].j+dirj[i];
}
st++;
}
st=st=1;
c[st].i=pji;
c[st].j=pjj;
while(st<=sf)
{
for(i=0;i<8;i++)
if(b[c[st].i][c[st].j]+1<b[c[st].i+diri[i]][c[st].j+dirj[i]])
{
sf++;
b[c[st].i+diri[i]][c[st].j+dirj[i]]=b[c[st].i][c[st].j]+1;
c[sf].i=c[st].i+diri[i];
c[sf].j=c[st].j+dirj[i];
}
st++;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if(a[i][j]==b[i][j] && a[i][j]!=-1 && a[i][j]!=2000)
g<<a[i][j]<<" "<<i+1<<" "<<j+1<<'\n', j=m,i=n;
}
return 0;
}