Pagini recente » Cod sursa (job #406363) | Cod sursa (job #447483) | Clasamentul arhivei de probleme | Cod sursa (job #3282475) | Cod sursa (job #2409483)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");
int n,m,x,i,y,l,in,sf,r[101][101],j[101][101],a[101][101];
string t;
struct coada
{
int lin,col;
}cdr[10001],cdj[100001];
int main()
{
f >> n >> m;
getline(f,t);
for(i=1; i<=n; i++) {
getline(f,t);
for(l=1; l<=m; l++) {
if(t[l-1]=='X') a[i][l]=1;
else if(t[l-1]=='R') {
r[i][l]=1;
cdr[1].lin=i;
cdr[1].col=l;
}
else if(t[l-1]=='J') {
j[i][l]=1;
cdj[1].lin=i;
cdj[1].col=l;
}
}
}
in=1;
sf=1;
while(in<=sf) {
x=cdr[in].lin;
y=cdr[in].col;
if(x-1>0 and a[x-1][y]==0 and r[x-1][y]==0) {
sf++;
r[x-1][y]=r[x][y]+1;
cdr[sf].lin=x-1;
cdr[sf].col=y;
}
if(x+1<=n and a[x+1][y]==0 and r[x+1][y]==0) {
sf++;
r[x+1][y]=r[x][y]+1;
cdr[sf].lin=x+1;
cdr[sf].col=y;
}
if(y-1>0 and a[x][y-1]==0 and r[x][y-1]==0) {
sf++;
r[x][y-1]=r[x][y]+1;
cdr[sf].lin=x;
cdr[sf].col=y-1;
}
if(y+1<=m and a[x][y+1]==0 and r[x][y+1]==0) {
sf++;
r[x][y+1]=r[x][y]+1;
cdr[sf].lin=x;
cdr[sf].col=y+1;
}
if(x-1>0 and y-1>0 and a[x-1][y-1]==0 and r[x-1][y-1]==0) {
sf++;
r[x-1][y-1]=r[x][y]+1;
cdr[sf].lin=x-1;
cdr[sf].col=y-1;
}
if(x-1>0 and y+1<=m and a[x-1][y+1]==0 and r[x-1][y+1]==0) {
sf++;
r[x-1][y+1]=r[x][y]+1;
cdr[sf].lin=x-1;
cdr[sf].col=y+1;
}
if(x+1<=n and y-1>0 and a[x+1][y-1]==0 and r[x+1][y-1]==0) {
sf++;
r[x+1][y-1]=r[x][y]+1;
cdr[sf].lin=x+1;
cdr[sf].col=y-1;
}
if(x+1<=n and y+1<=m and a[x+1][y+1]==0 and r[x+1][y-+1]==0) {
sf++;
r[x+1][y+1]=r[x][y]+1;
cdr[sf].lin=x+1;
cdr[sf].col=y+1;
}
in++;
}
in=1;
sf=1;
while(in<=sf) {
x=cdj[in].lin;
y=cdj[in].col;
if(x-1>0 and a[x-1][y]==0 and j[x-1][y]==0) {
sf++;
j[x-1][y]=j[x][y]+1;
cdj[sf].lin=x-1;
cdj[sf].col=y;
}
if(x+1<=n and a[x+1][y]==0 and j[x+1][y]==0) {
sf++;
j[x+1][y]=j[x][y]+1;
cdj[sf].lin=x+1;
cdj[sf].col=y;
}
if(y-1>0 and a[x][y-1]==0 and j[x][y-1]==0) {
sf++;
j[x][y-1]=j[x][y]+1;
cdj[sf].lin=x;
cdj[sf].col=y-1;
}
if(y+1<=m and a[x][y+1]==0 and j[x][y+1]==0) {
sf++;
j[x][y+1]=j[x][y]+1;
cdj[sf].lin=x;
cdj[sf].col=y+1;
}
if(x-1>0 and y-1>0 and a[x-1][y-1]==0 and j[x-1][y-1]==0) {
sf++;
j[x-1][y-1]=j[x][y]+1;
cdj[sf].lin=x-1;
cdj[sf].col=y-1;
}
if(x-1>0 and y+1<=m and a[x-1][y+1]==0 and j[x-1][y+1]==0) {
sf++;
j[x-1][y+1]=j[x][y]+1;
cdj[sf].lin=x-1;
cdj[sf].col=y+1;
}
if(x+1<=n and y-1>0 and a[x+1][y-1]==0 and j[x+1][y-1]==0) {
sf++;
j[x+1][y-1]=j[x][y]+1;
cdj[sf].lin=x+1;
cdj[sf].col=y-1;
}
if(x+1<=n and y+1<=m and a[x+1][y+1]==0 and j[x+1][y+1]==0) {
sf++;
j[x+1][y+1]=j[x][y]+1;
cdj[sf].lin=x+1;
cdj[sf].col=y+1;
}
in++;
}
return 0;
}