Pagini recente » Cod sursa (job #1737659) | Cod sursa (job #2296902) | Cod sursa (job #1393652) | Profil tlapusan | Cod sursa (job #1828589)
#include<iostream>
#include<fstream>
#include<cmath>
#include<string.h>
using namespace std;
struct Punct{
int x,y;
};
ofstream g("rj.out");
char map[110][110];
int n,m;
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={-1,0,1,0,-1,1,1,-1};
int L[101][110];
Punct Romeo,Julieta;
void citireDate(){
ifstream f("rj.in");
f>>n>>m;
char c;
f.get(c);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
f.get(c);
map[i][j]=c;
if(map[i][j]=='R'){
Romeo.x=i;
Romeo.y=j;
}
else
if(map[i][j]=='J'){
Julieta.x=i;
Julieta.y=j;
}
}
f.get(c);
}
}
int conditii(Punct p1,Punct p2){
if(L[p1.x][p1.y]>0&&L[p2.x][p2.y]>0)
return 0;
if(L[p1.x][p1.y]<0&&L[p2.x][p2.y]<0)
return 0;
if(abs(L[p1.x][p1.y])!=abs(L[p2.x][p2.y])+1)
return 0;
return 1;
}
int verificarePozitie(Punct p){
if(map[p.x][p.y]!=' ')
return 0;
if(L[p.x][p.y]!=0)
return 0;
if(!(p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m))
return 0;
return 1;
}
void Lee(){
Punct Coada[20000];
int prim=1,ultim=2;
Coada[1]=Romeo;
Coada[2]=Julieta;
L[Romeo.x][Romeo.y]=-1;
L[Julieta.x][Julieta.y]=1;
Punct punctIntalnire;
punctIntalnire.x=0;
punctIntalnire.y=0;
while(prim<=ultim&&punctIntalnire.x==0&&punctIntalnire.y==0){
Punct punctCurent=Coada[prim++];
for(int i=0;i<=7;i++){
Punct punctUrmator;
punctUrmator.x=punctCurent.x+dx[i];
punctUrmator.y=punctCurent.y+dy[i];
if(verificarePozitie(punctUrmator)){
if(L[punctCurent.x][punctCurent.y]>0)
L[punctUrmator.x][punctUrmator.y]=L[punctCurent.x][punctCurent.y]+1;
else
if(L[punctCurent.x][punctCurent.y]<0)
L[punctUrmator.x][punctUrmator.y]=L[punctCurent.x][punctCurent.y]-1;
Coada[++ultim]=punctUrmator;
}
else
if(conditii(punctUrmator,punctCurent)){
punctIntalnire.x=punctUrmator.x;
punctIntalnire.y=punctUrmator.y;
}
}
}
g<<abs(L[punctIntalnire.x][punctIntalnire.y])<<" "<<punctIntalnire.x<<" "<<punctIntalnire.y;
}
int main(){
citireDate();
Lee();
}