Pagini recente » Cod sursa (job #1674970) | Cod sursa (job #1900603) | Cod sursa (job #867619) | Cod sursa (job #1690670) | Cod sursa (job #1120414)
#include<fstream>
#include<queue>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int n,m,a[110][110],x1,y1,x2,y2,p,q;
int dx[14]={0,-1,0,1,-1,1,-1,0,1};
int dy[14]={0,-1,-1,-1,0,0,1,1,1};
string s;
bool incoada;
struct point
{
int x,y;
};
point b,curent;
queue < point > coada;
void lee(int x1,int y1)
{
b.x=x1;
b.y=y1;
a[x1][y1]=1;
coada.push(b);
while(!coada.empty())
{
curent=coada.front();
coada.pop();
for(int k=1;k<=8;++k)
{
p=curent.x+dx[k];
q=curent.y+dy[k];
if(p>=1 && p<=n && q>=1 && q<=m &&(a[p][q]==0 || a[p][q]>a[curent.x][curent.y]+1))
{
b.x=p;
b.y=q;
a[p][q]=a[curent.x][curent.y]+1;
//if(incoada==false)
//{
coada.push(b);
//incoada=true;
//}
}
}
}
}
int main()
{
f>>n>>m;
getline(f,s);
for(int i=1;i<=n;++i)
{
getline(f,s);
for(int j=0;j<=m-1;++j)
{
if(s[j]=='X') a[i][j+1]=-1;
//if(s[i]==' ') a[i][j]=0;
if(s[j]=='J')
{
x1=i;y1=j+1;
}
if(s[j]=='R')
{
x2=i;y2=j+1;
}
}
}
lee(x1,y1);
g<<(a[x2][y2]+1)/2<<' ';
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(a[i][j]==(a[x2][y2]+1)/2) g<<i<<' '<<j<<'\n';
f.close();g.close();
return 0;
}