Pagini recente » Cod sursa (job #575020) | Cod sursa (job #2412748) | Cod sursa (job #1850612) | Cod sursa (job #2243869) | Cod sursa (job #1805648)
#include <iostream>
#include <fstream>
#include <queue>
#include <string.h>
using namespace std;
int n, m, tmin=1000010, ox, oy, ix1, iy1, ix2, iy2, romeo[107][107], julieta[107][107]; char c;
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};
ofstream g("rj.out");
void read()
{
ifstream f("rj.in");
f >> n >> m; f.get();
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
{
f.get(c); cout<<c+1<<" ";
if(c=='R')
{ix1=i;
iy1=j; romeo[i][j]=1;
}
else
if(c=='J')
{ ix2=i;
iy2=j; julieta[i][j]=1;
}
else
if(c=='X')
{
romeo[i][j]=-1;
julieta[i][j]=-1;
}
}
f.get(); cout<<endl;
}
/** for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
g<<romeo[i][j]<<" ";
g<<endl;
}
g<<endl;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
g<<julieta[i][j]<<" ";
g<<endl;
} **/
f.close();
}
void lee1()
{
for(int i=0; i<=n+1; ++i) romeo[i][0]=romeo[i][n+1]=-1;
for(int i=0; i<=m+1; ++i) romeo[0][i]=romeo[m+1][i]=-1;
pair <int, int> xx, yy;
q.push(make_pair(ix1, iy1));
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(romeo[yy.first][yy.second] == 0)
{
romeo[yy.first][yy.second] = romeo[xx.first][xx.second] + 1;
q.push(make_pair(yy.first, yy.second));
}
}
}
}
void lee2()
{
for(int i=0; i<=n+1; ++i) julieta[i][0]=julieta[i][n+1]=-1;
for(int i=0; i<=m+1; ++i) julieta[0][i]=julieta[m+1][i]=-1;
pair <int, int> xx, yy;
q.push(make_pair(ix2, iy2));
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(julieta[yy.first][yy.second] == 0)
{
julieta[yy.first][yy.second] = julieta[xx.first][xx.second] + 1;
q.push(make_pair(yy.first, yy.second));
}
}
}
}
void cauta()
{
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
{
if(romeo[i][j]==julieta[i][j] && romeo[i][j]>0 && romeo[i][j]<tmin)
{
ox=i; oy=j; tmin=romeo[i][j];
}
}
}
}
void out()
{
g << tmin <<' '<< ox <<' '<< oy <<'\n';
/** for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
g<<romeo[i][j]<<" ";
g<<endl;
}
g<<endl;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
g<<julieta[i][j]<<" ";
g<<endl;
} **/
g.close();
}
int main()
{
read();
lee1();
lee2();
cauta();
out();
return 0;
}