Pagini recente » Cod sursa (job #1479930) | Cod sursa (job #1028981) | Cod sursa (job #302624) | Cod sursa (job #256866) | Cod sursa (job #887008)
Cod sursa(job #887008)
#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,poz=1;
short int solutie()
{
short int i, j;
short int cs=9999;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(mat1[i][j]==mat2[i][j])
if(mat1[i][j]>0)
{
if(cs>mat1[i][j])
{cs=mat1[i][j]; cc=i; ccc=j;}
}
return cs;
}
void lee1(short int x, short int y)
{
short int i,k;
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);
for(i=0; i<poz; i++)
{
cx[i]=0;
cy[i]=0;
}
lee2(xj, yj);
minn=solutie();
out<<minn<<" "<<cc+1<<" "<<ccc+1;
in.close();
out.close();
return 0;
}