Pagini recente » Cod sursa (job #2991) | Cod sursa (job #2906739) | Cod sursa (job #140568) | Cod sursa (job #534919) | Cod sursa (job #3215569)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("RJ.in");
ofstream fout("RJ.out");
struct point{
int x;
int y;
};
int dir_x[8]={0,-1,1,0,-1,1,-1,1};
int dir_y[8]={-1,0,0,1,-1,-1,1,1};
int dist[105][105];
int blocat[105][105];
queue<point>q;
string s;
int n,m;
bool in_mat(point poz){
return poz.x>=1 && poz.x<=n && poz.y>=1 && poz.y<=m;
}
void lee(point start){
dist[start.x][start.y]=1;
q.push(start);
while(!q.empty()){
point poz_in=q.front();
q.pop();
for(int i=0;i<8;i++){
point poz_urm;
poz_urm=poz_in;
poz_urm.x+=dir_x[i];
poz_urm.y+=dir_y[i];
if(in_mat(poz_urm) && dist[poz_urm.x][poz_urm.y]==0 && blocat[poz_urm.x][poz_urm.y]==0){
dist[poz_urm.x][poz_urm.y]=dist[poz_in.x][poz_in.y]+1;
q.push(poz_urm);
}
}
}
}
int main()
{
fin>>n>>m;
point R;
point J;
getline(fin,s);
for(int i=1;i<=n;i++){
getline(fin,s);
//cout<<s<<endl;
for(int j=0;j<m;j++){
if(s[j]=='R'){
R.x=i;
R.y=j+1;
}
else if(s[j]=='J'){
J.x=i;
J.y=j+1;
}
else if(s[j]=='X'){
blocat[i][j+1]=1;
}
}
}
lee(R);
int t=dist[J.x][J.y];
if(t%2==0)
t/=2;
else
t=t/2+1;
fout<<t<<" ";
int X,Y;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(dist[i][j]==t){
X=i;
Y=j;
}
//cout<<dist[i][j]<<" ";
}
//cout<<endl;
}
fout<<X<<" "<<Y;
return 0;
}