Pagini recente » Cod sursa (job #606745) | Cod sursa (job #2852234) | Cod sursa (job #1625758) | Cod sursa (job #2237560) | Cod sursa (job #1072686)
#include <fstream>
using namespace std;
char ch, s[200];
int solx,soly,tmp,n,m,i,j,p,u,d,ic,jc,iv,jv;
int c[2][10005];
int mat1[105][105],mat2[105][105];
struct data {
int l;
int c;
} r,jl;
int di[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dj[] = {1, -1, 1, 0, -1, 1, 0, -1};
int main() {
ifstream f("rj.in");
ofstream g("rj.out");
f>>m>>n;
for(i=1;i<=m;i++) {
f.get();
f.get(s,200);
for(j=1;j<=n;j++) {
ch=s[j-1];
if(ch=='R') {
mat1[i][j]=1;
r.l=i;r.c=j;
}
else
if(ch=='J') {
mat2[i][j]=1;
jl.l=i;jl.c=j;
}
else
if(ch=='X')
mat1[i][j]=mat2[i][j]=-1;
}
}
p=1;u=1;
c[0][1]=r.l;
c[1][1]=r.c;
while(p<=u) {
ic=c[0][p];
jc=c[1][p];
for(d=0;d<8;d++){
iv=ic+di[d];
jv=jc+dj[d];
if(iv<=m && iv>=1 && jv>=1 && jv<=n && mat1[iv][jv]==0) {
mat1[iv][jv]=mat1[ic][jc]+1;
u++;
c[0][u]=iv;
c[1][u]=jv;
}
}
p++;
}
p=1;u=1;
c[0][1]=jl.l;
c[1][1]=jl.c;
while(p<=u) {
ic=c[0][p];
jc=c[1][p];
for(d=0;d<8;d++){
iv=ic+di[d];
jv=jc+dj[d];
if(iv<=m && iv>=1 && jv>=1 && jv<=n && mat2[iv][jv]==0) {
mat2[iv][jv]=mat2[ic][jc]+1;
u++;
c[0][u]=iv;
c[1][u]=jv;
}
}
p++;
}
tmp=100000;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(mat1[i][j]==mat2[i][j] && mat1[i][j]!=-1 && mat1[i][j]<tmp && mat1[i][j]!=0) {
tmp=mat1[i][j];
solx=i;
soly=j;
}
g<<tmp<<" "<<solx<<" "<<soly<<"\n";
return 0;
}