Pagini recente » Cod sursa (job #502630) | Cod sursa (job #840742) | Cod sursa (job #1388115) | Cod sursa (job #1556504) | Cod sursa (job #2269566)
#include <iostream>
#include <fstream>
#include <queue>
#include <climits>
using namespace std;
int a[100][100],b[100][100];
queue <pair <int,int> > Q;
void bordare(int n, int m, int a[100][100])
{
for(int i=0; i<=n+1; i++)
{
a[i][0]=-1;
a[i][m+1]=-1;
}
for(int j=0; j<=m+1; j++)
{
a[0][j]=-1;
a[n+1][j]=-1;
}
}
int di[]= {-1, -1, 0, 1, 1, 1, 0, -1};
int dj[]= {0, 1, 1, 1, 0, -1, -1, -1};
void lee(int x,int y,int a[100][100])
{
a[x][y]=1;
Q.push(make_pair(x,y));
while(!Q.empty())
{
pair<int,int> p=Q.front();
Q.pop();
for(int k=0; k<8; k++)
{
if(a[p.first+di[k]][p.second+dj[k]]==0)
{
a[p.first+di[k]][p.second+dj[k]] = 1 + a[p.first][p.second];
Q.push(make_pair(p.first+di[k] , p.second+dj[k]));
}
}
}
}
int main()
{
ifstream f("rj.in");
ofstream g("rj.out");
int N,M,ir,jr,ij,jj,mini=INT_MAX;
char v[256];
f>>N>>M;
bordare(N,M,a);
bordare(N,M,b);
f.get();
for(int i=1; i<=N; i++)
{
f.getline(v,256);
for(int j=1; j<=M; j++)
{
if(v[j-1]=='X') {a[i][j]=-1; b[i][j]=-1;}
else if(v[j-1]==' ') {a[i][j]=0;b[i][j]=0;}
else {if(v[j-1]=='R')
{
ir=i;
jr=j;
a[i][j]=0;
b[i][j]=0;
}
else
{
ij=i;
jj=j;
a[i][j]=0;
b[i][j]=0;
}
}
}
}
lee(ir,jr,a);
lee(ij,jj,b);
int pozi, pozj;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
if(a[i][j]==b[i][j]&&a[i][j]!=-1&&a[i][j]!=0)
if(a[i][j]<mini)
{mini=a[i][j]; pozi=i;
pozj=j;}
cout<<mini<<" "<<pozi<<" "<<pozj;
return 0;
}