Pagini recente » Cod sursa (job #1966162) | Cod sursa (job #1765651) | Cod sursa (job #1328016) | Cod sursa (job #801289) | Cod sursa (job #2534593)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
struct lincol{
int l,c;
}pos,poz;
queue<lincol>ro;
queue<lincol>ju;
int n,m,mat[105][105];
char ch[105],c;
int pi[10]={1,1,1,0,0,-1,-1,-1};
int pc[10]={1,0,-1,-1,1,1,0,-1};
int main()
{
fin>>n>>m;
fin.get();
for(int i=1;i<=n;i++){
fin.getline(ch,105);
for(int j=0;j<m;j++){
c=ch[j];
switch(c){
case 'R':
pos.l=i;
pos.c=j+1;
ro.push(pos);
mat[i][j+1]=1;
break;
case 'J':
pos.l=i;
pos.c=j+1;
ju.push(pos);
mat[i][j+1]=10000;
break;
case 'X':
mat[i][j+1]=-1;
}
}
}
for(int i=0;i<=n+1;i++){
mat[i][0]=-1;
mat[i][m+1]=-1;
}
for(int i=0;i<=m+1;i++){
mat[0][i]=-1;
mat[n+1][i]=-1;
}
while(!ro.empty() && !ju.empty()){
pos=ro.front();
ro.pop();
for(int k=0;k<=7;k++){
poz.l=pos.l+pi[k];
poz.c=pos.c+pc[k];
if(mat[poz.l][poz.c]==0){
mat[poz.l][poz.c]=mat[pos.l][pos.c]+1;
ro.push(poz);
}
if(mat[poz.l][poz.c]>=10000){
fout<<mat[pos.l][pos.c]<<' '<<pos.l<<' '<<pos.c;
return 0;
}
}
pos=ju.front();
ju.pop();
for(int k=0;k<=7;k++){
poz.l=pos.l+pi[k];
poz.c=pos.c+pc[k];
if(mat[poz.l][poz.c]==0){
mat[poz.l][poz.c]=mat[pos.l][pos.c]+1;
ju.push(poz);
}
if(mat[poz.l][poz.c]<10000 && mat[poz.l][poz.c]>=1){
fout<<mat[pos.l][pos.c]<<' '<<pos.l<<' '<<pos.c;
return 0;
}
}
}
return 0;
}