Pagini recente » Cod sursa (job #1551560) | Cod sursa (job #2030076) | Cod sursa (job #328401) | Cod sursa (job #1872557) | Cod sursa (job #1805958)
#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 a[105][105];
queue <pair <int, int> > q1,q2;
const int dx[]={0,1,1,1,0,-1,-1,-1};
const int dy[]={1,1,0,-1,-1,-1,0,1};
char c[107];
void read()
{
ifstream f("rj.in");
f >> n >> m;
for(int i=1; i<=n; i++)
{
f.get();
f.get(c,107);
for(int j=0; j<m; ++j)
{
if(c[j]=='R')
{
ix1=i; iy1=j+1; q1.push(make_pair(ix1, iy1)); romeo[i][j+1]=1;
}
else if(c[j]=='J')
{
ix2=i; iy2=j+1; q2.push(make_pair(ix2, iy2)); julieta[i][j+1]=1;
}
else if(c[j]=='X')
{
romeo[i][j+1]=-1;
julieta[i][j+1]=-1;
}
}
}
for(int i=0; i<=n+1; ++i) romeo[i][0]=romeo[i][n+1]=julieta[i][0]=julieta[i][n+1]=-1;
for(int i=0; i<=m+1; ++i) romeo[0][i]=romeo[m+1][i]=julieta[0][i]=julieta[m+1][i]=-1;
f.close();
}
void lee1()
{
pair <int, int> xx, yy;
while(!q1.empty())
{
xx=q1.front(); q1.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;
q1.push(make_pair(yy.first, yy.second));
}
}
}
}
void lee2()
{
pair <int, int> xx, yy;
while(!q2.empty())
{
xx=q2.front(); q2.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;
q2.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()
{
ofstream g("rj.out");
g << tmin <<' '<< ox <<' '<< oy <<'\n';
g.close();
}
int main()
{
read();
lee1();
lee2();
cauta();
out();
return 0;
}