Pagini recente » Cod sursa (job #1455671) | Cod sursa (job #461445) | Cod sursa (job #142170) | Cod sursa (job #578050) | Cod sursa (job #1629570)
#include <iostream>
#include <fstream>
#include <queue>
#include <iomanip>
#include <string>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int ok,n,m,i,j,cont,r[111][111],ju[111][111],ir,jr,ij,jj,di[]={-1,-1,-1,0,1,1,1,0},dj[]={-1,0,1,1,1,0,-1,-1};
queue <pair <int, int> > coada;
char a[111][111];
string s ;
void leer()
{
int i,j,i_urma,j_urma;
r[ir][jr]=1;
coada.push(make_pair(ir,jr));
while(!coada.empty())
{
i=coada.front().first;
j=coada.front().second;
coada.pop();
for(int directie=0;directie<8;directie++)
{
if(directie==0 and a[i][j-1]=='X' and a[i-1][j]=='X')
directie++;
if(directie==2 and a[i-1][j]=='X' and a[i][j+1]=='X')
directie++;
if(directie==4 and a[i][j+1]=='X' and a[i+1][j]=='X')
directie++;
if(directie==6 and a[i+1][j]=='X' and a[i][j-1]=='X')
directie++;
i_urma=i+di[directie];
j_urma=j+dj[directie];
if(a[i_urma][j_urma]!='X' and r[i_urma][j_urma]==0 /*and ()*/)
{
r[i_urma][j_urma]=r[i][j]+1;
coada.push(make_pair(i_urma,j_urma));
}
}
}
}
void leej()
{
ok=0;
int i,j,i_urma,j_urma;
ju[ij][jj]=1;
coada.push(make_pair(ij,jj));
while(!coada.empty())
{
i=coada.front().first;
j=coada.front().second;
coada.pop();
for(int directie=0;directie<8;directie++)
{
if(directie==0 and a[i][j-1]=='X' and a[i-1][j]=='X')
directie++;
if(directie==2 and a[i-1][j]=='X' and a[i][j+1]=='X')
directie++;
if(directie==4 and a[i][j+1]=='X' and a[i+1][j]=='X')
directie++;
if(directie==6 and a[i+1][j]=='X' and a[i][j-1]=='X')
directie++;
i_urma=i+di[directie];
j_urma=j+dj[directie];
if(a[i_urma][j_urma]!='X' and ju[i_urma][j_urma]==0 /*and ()*/)
{
ju[i_urma][j_urma]=ju[i][j]+1;
if(ju[i_urma][j_urma]==r[i_urma][j_urma])
{
fout<<ju[i_urma][j_urma]<<" "<<i_urma<<" "<<j_urma;
ok=1;
break;
}
coada.push(make_pair(i_urma,j_urma));
}
}
if(ok==1)
break;
}
}
int main()
{
getline(fin,s);
n=s[0]-48;
m=s[2]-48;
for(i=1;i<=n;i++)
{
getline(fin,s);
for(j=1;j<=m;j++)
{
a[i][j]=s[j-1];
if(a[i][j]=='R')
{
ir=i;
jr=j;
}
if(a[i][j]=='J')
{
ij=i;
jj=j;
}
}
}
for(i=0;i<=n+1;i++)
{
a[i][0]='X';
a[i][m+1]='X';
}
for(i=0;i<=m+1;i++)
{
a[0][i]='X';
a[n+1][i]='X';
}
leer();
leej();
return 0;
}