Pagini recente » Cod sursa (job #384469) | Cod sursa (job #296974) | Cod sursa (job #2887044) | Cod sursa (job #124865) | Cod sursa (job #1371738)
#include <fstream>
#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];
int n,m;
queue <poin> co;
void lee1()
{
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<=m&&o.y>=1)
{
a[o.x][o.y]=a[t.x][t.y]+1;
co.push(o);
}
}
}
}
void lee2()
{
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<=m&&o.y>=1)
{
b[o.x][o.y]=b[t.x][t.y]+1;
co.push(o);
}
}
}
}
int main()
{
ifstream f("rj.in");
ofstream g("rj.out");
int i,j,min=INT_MAX;
f>>n>>m;
f.get();
char *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];
for (i=1;i<=n;i++)
{
f.get(s,102);
f.get();
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();
co.push(ju);
lee2();
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;}
}
}
}
g<<min<<' '<<k.x<<' '<<k.y<<'\n';
f.close();
f.close();
return 0;
}