Pagini recente » Cod sursa (job #830958) | Cod sursa (job #1425074) | Cod sursa (job #1844966) | Cod sursa (job #1425072) | Cod sursa (job #2376141)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int N,M,MapF1[105][105],MapF2[105][105],MapR[105][105],MapJ[105][105];
char lin[105];
char Map[105][105];
short dl[]={-1,0,1,0,-1,1,1,-1};
short dc[]={0,1,0,-1,-1,1,-1,1};
pair<int,int>R,J;
void Citire()
{
fin>>N>>M;
fin.get();
for(int i=1;i<=N;i++)
{
fin.getline(lin,M+1);
for(int j=0;j<=M;j++)
{
if(lin[j]=='X')
{
MapF1[i][j+1]=0;
MapF2[i][j+1]=0;
}
if(lin[j]==' ')
{
MapF1[i][j+1]=1;
MapF2[i][j+1]=1;
}
if(lin[j]=='R')
{
MapF1[i][j+1]=2;
MapF2[i][j+1]=2;
R.first=i;
R.second=j+1;
}
if(lin[j]=='J')
{
MapF1[i][j+1]=2;
MapF2[i][j+1]=2;
J.first=i;
J.second=j+1;
}
}
}
}
void Afisare()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
fout<<MapF1[i][j]<<" ";
fout<<endl;
}
}
void AfisareR()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
fout<<MapR[i][j]<<" ";
fout<<endl;
}
}
void AfisareJ()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
fout<<MapJ[i][j]<<" ";
fout<<endl;
}
}
bool OkR(int x, int y)
{
if(x<1 || y<1 || x>N || y>N || MapF1[x][y]==0)
return false;
return true;
}
bool OkJ(int x, int y)
{
if(x<1 || y<1 || x>N || y>N || MapF2[x][y]==0)
return false;
return true;
}
void LeeR(int x, int y)
{
queue< pair< int, int > >Q;
pair< int,int > P,U;
MapR[x][y]=1;
MapF1[x][y]=0;
Q.push({x,y});
while(!Q.empty())
{
P=Q.front();
Q.pop();
for(int z=0; z<8;z++)
{
if(OkR(P.first+dl[z],P.second+dc[z]))
{
U.first =P.first +dl[z];
U.second=P.second+dc[z];
MapF1[U.first][U.second]=0;
MapR[U.first][U.second]=MapR[P.first][P.second]+1;
Q.push(U);
}
}
}
}
void LeeJ(int x, int y)
{
int tmin=999,imin=999,jmin=999;
queue< pair< int, int > >Q;
pair< int,int > P,U;
MapJ[x][y]=1;
MapF2[x][y]=0;
Q.push({x,y});
while(!Q.empty())
{
P=Q.front();
Q.pop();
for(int z=0; z<8;z++)
{
if(OkJ(P.first+dl[z],P.second+dc[z]))
{
U.first =P.first +dl[z];
U.second=P.second+dc[z];
MapF2[U.first][U.second]=0;
MapJ[U.first][U.second]=MapJ[P.first][P.second]+1;
if(MapJ[U.first][U.second]==MapR[U.first][U.second])
{
if(imin>U.first)
imin=U.first;
if(jmin>U.second)
jmin=U.second;
if(tmin>MapJ[U.first][U.second])
tmin=MapJ[U.first][U.second];
}
Q.push(U);
}
}
}
fout<<tmin<<" "<<imin<<" "<<jmin;
}
int main()
{
Citire();
LeeR(R.first,R.second);
LeeJ(J.first,J.second);
return 0;
}