Pagini recente » Cod sursa (job #720451) | Cod sursa (job #3208107) | Cod sursa (job #2621956) | Cod sursa (job #37979) | Cod sursa (job #1371729)
#include <cstdio>
#include <queue>
#include <climits>
using namespace std;
int a[105][105];
int b[105][105];
typedef struct{int x,y;} poin;
poin r,ju,k;
poin d[9];
queue <poin> co;
void lee1(int n)
{
poin t,o;int j;
while (!co.empty())
{
t=co.front();
co.pop();
for (j=1;j<=8;j++)
{
o.x=t.x+d[j].x;
o.y=t.y+d[j].y;
if (!a[o.x][o.y]&&o.x>=1&&o.x<=n&&o.y<=n&&o.y>=1)
{
a[o.x][o.y]=a[t.x][t.y]+1;
co.push(o);
}
}
}
}
void lee2(int n)
{
poin t,o;int j;
while (!co.empty())
{
t=co.front();
co.pop();
for (j=1;j<=8;j++)
{
o.x=t.x+d[j].x;
o.y=t.y+d[j].y;
if (!b[o.x][o.y]&&o.x>=1&&o.x<=n&&o.y<=n&&o.y>=1)
{
b[o.x][o.y]=b[t.x][t.y]+1;
co.push(o);
}
}
}
}
int main()
{
FILE *f,*g;
f=freopen("rj.in","r",stdin);
g=freopen("rj.out","w",stdout);
int n,m,i,j,min=INT_MAX;
fscanf(f,"%d%d",&n,&m);
char c,*s;
{
d[6].y=d[7].y=d[8].y=d[1].x=d[2].x=d[8].x=-1;
d[2].y=d[3].y=d[4].y=d[4].x=d[5].x=d[6].x=1;
}
s=new char[105];
gets(s);
for (i=1;i<=n;i++)
{
gets(s);
for (j=1;j<=m;j++)
if (s[j-1]=='X') b[i][j]=a[i][j]=-1;
else
if (s[j-1]=='R') {r.x=i;r.y=j;a[i][j]=1;}
else
if (s[j-1]=='J') {ju.x=i;ju.y=j;b[i][j]=1;}
}
co.push(r);
lee1(n);
co.push(ju);
lee2(n);
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
if (a[i][j]>0&&a[i][j]==b[i][j])
{
if (a[i][j]<min) {min=a[i][j];k.x=i;k.y=j;}
}
}
}
fprintf(g,"%d %d %d\n",min,k.x,k.y);
fclose(f);
fclose(g);
return 0;
}