Pagini recente » Cod sursa (job #1930214) | Cod sursa (job #2729322) | Cod sursa (job #682327) | Cod sursa (job #721238) | Cod sursa (job #1805497)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
int n, m, tmin, a[105][105], xo=105, yo=105;
queue <pair <int, int> > q;
const int dx[]={0,1,1,1,0,-1,-1,-1};
const int dy[]={1,1,0,-1,-1,-1,0,1};
void read()
{
ifstream f("rj.in");
f >> n >> m; char aux[105], c;
for(int i=1; i<=n; ++i)
{ f.get(); f.get(aux,101);
for(int j=0; j<m; ++j)
{
c=aux[j];
if(c=='R')
{q.push(make_pair(i,j+1)); a[i][j+1]=1;}
if(c=='J')
{q.push(make_pair(i,j+1)); a[i][j+1]=1;}
if(c==' ')
a[i][j+1]=0;
if(c=='X')
a[i][j+1]=-1;
}
}
f.close();
}
void lee()
{
for(int i=0; i<=n+1; ++i) a[i][0]=a[i][n+1]=-1;
for(int i=0; i<=m+1; ++i) a[0][i]=a[m+1][i]=-1;
pair <int, int> xx, yy;
while(!q.empty())
{
xx=q.front(); q.pop();
for(int i=0; i<8; ++i)
{
yy.first = xx.first + dx[i];
yy.second = xx.second +dy[i];
if(a[yy.first][yy.second] == 0)
{
a[yy.first][yy.second] = a[xx.first][xx.second] + 1;
q.push(make_pair(yy.first, yy.second));
}
else
if(a[yy.first][yy.second] == a[xx.first][xx.second] + 1)
{
if(yy.second < yo)
{
xo = yy.first;
yo = yy.second;
tmin = a[yy.first][yy.second];
}
}
}
}
}
void out()
{
ofstream g("rj.out");
g << tmin <<' '<< xo <<' '<< yo <<'\n';
g.close();
}
int main()
{
read();
lee();
out();
return 0;
}