Pagini recente » Cod sursa (job #516700) | Cod sursa (job #2305427) | Cod sursa (job #2639621) | Cod sursa (job #1723035) | Cod sursa (job #1857371)
#include <fstream>
#include <queue>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
const int N=105;
int n,m,matr[N][N],matj[N][N],dx[]={-1,-1,0,1,1,1,0,-1},dy[]={0,1,1,1,0,-1,-1,-1},Min=N*N;
char rand[N];
bool bun(int i,int j){
return (i&&j&&i<=n&&j<=m);
}
void Lee(int mat[N][N],int xi,int yi){
int ii,jj;
queue <int> cx,cy;
mat[xi][yi]=1;
cx.push(xi);
cy.push(yi);
while(!cx.empty()){
for(int k=0;k<8;k++){
ii=cx.front()+dx[k];
jj=cy.front()+dy[k];
if(bun(ii,jj)&&mat[ii][jj]==0){
mat[ii][jj]=mat[cx.front()][cy.front()]+1;
cx.push(ii);
cy.push(jj);
}
}
cx.pop();
cy.pop();
}
}
int main()
{
int xi,yi,xf,yf,c1,c2;
char x;
in>>n>>m;
in.get();
for(int i=1;i<=n;i++){
in.getline(rand,N,'\n');
for(int j=0;j<m;j++){
if(rand[j]=='X')
matr[i][j+1]=matj[i][j+1]=-1;
else if(rand[j]=='R'){
xi=i;
yi=j+1;
}
else if(rand[j]=='J'){
xf=i;
yf=j+1;
}
}
}
in.close();
Lee(matr,xi,yi);
Lee(matj,xf,yf);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(matr[i][j]==matj[i][j]&&matr[i][j]>0&&matr[i][j]<Min){
Min=matr[i][j];
c1=i;
c2=j;
}
out<<Min<<" "<<c1<<" "<<c2<<"\n";
out.close();
return 0;
}