Pagini recente » Cod sursa (job #2201045) | Cod sursa (job #432440) | Cod sursa (job #742779) | Cod sursa (job #2154946) | Cod sursa (job #886974)
Cod sursa(job #886974)
#include<iostream>
#include<fstream>
using namespace std;
short int mat1[101][101],cx[10000],cy[10000],n,m;
short int mat2[101][101];
const short int dx[]={1,1,1,0,0,-1,-1,-1};
const short int dy[]={0,1,-1,1,-1,1,0,-1};
short int cc,ccc;
short int solutie()
{
for(cc=0; cc<n; cc++)
for(ccc=0; ccc<n; ccc++)
if(mat1[cc][ccc]==mat2[cc][ccc])
if(mat1[cc][ccc]>0)
return mat1[cc][ccc];
}
void lee1(short int x, short int y)
{
short int i,k,poz=1;
cx[0]=x;
cy[0]=y;
for(i=0;i<poz; i++)
{
x=cx[i];
y=cy[i];
for(k=0; k<8; k++)
{
if(x+dx[k]>-1 && x+dx[k]<n && y+dy[k]>-1 && y+dy[k]<m)
if(mat1[x+dx[k]][y+dy[k]]==0)
{
cx[poz]=x+dx[k];
cy[poz]=y+dy[k];
poz++;
mat1[x+dx[k]][y+dy[k]]=mat1[x][y]+1;
}
}
}
}
void lee2(short int x, short int y)
{
short int i,k,poz=1;
cx[0]=x;
cy[0]=y;
for(i=0;i<poz; i++)
{
x=cx[i];
y=cy[i];
for(k=0; k<8; k++)
{
if(x+dx[k]>-1 && x+dx[k]<n && y+dy[k]>-1 && y+dy[k]<m)
if(mat2[x+dx[k]][y+dy[k]]==0)
{
cx[poz]=x+dx[k];
cy[poz]=y+dy[k];
poz++;
mat2[x+dx[k]][y+dy[k]]=mat2[x][y]+1;
}
}
}
}
int main()
{
short int i, j,xr,yr,yj,xj,minn;
string x;
ifstream in("rj.in");
ofstream out("rj.out");
in>>n>>m;
getline(in,x);
for(i=0; i<n; i++)
{
getline(in, x);
for(j=0; j<m; j++)
{
if(x[j]=='X'){
mat1[i][j]=-1; mat2[i][j]=-1;}
if(x[j]=='R')
{
mat1[i][j]=1;
xr=i;
yr=j;
}
if(x[j]=='J')
{
mat2[i][j]=1;
xj=i;
yj=j;
}
}
}
lee1(xr, yr);
lee2(xj, yj);
minn=solutie();
out<<minn<<" "<<cc+1<<" "<<ccc+1;
in.close();
out.close();
return 0;
}