Pagini recente » Cod sursa (job #2319840) | Cod sursa (job #2577033) | Cod sursa (job #2721768) | Cod sursa (job #1355856) | Cod sursa (job #1131006)
#include <iostream>
#include <fstream>
#include <string.h>
#include <queue>
using namespace std;
ifstream ka("rj.in");
ofstream ki("rj.out");
const int N_MAX = 100;
char a[N_MAX+1][N_MAX+1];
int n, m, b[N_MAX+1][N_MAX+1], c[N_MAX+1][N_MAX+1], minim = 10005;
int directiix[8] = {1, -1, 0, 0, 1, 1, -1, -1};
int directiiy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
struct punct
{
int x,y;
}romeo, julieta, raspuns;
queue <punct> coada;
void adaugare(int x, int y, int pas, bool r)
{
if(x > 0 && x <= n && y >= 0 && y < m)
{
punct p;
p.x = x;
p.y = y;
coada.push(p);
if(r)
b[x][y] = pas;
else
c[x][y] = pas;
}
}
void lee(punct p, bool r)
{
adaugare(p.x, p.y, 1, r);
while(!coada.empty())
{
punct p = coada.front();
int x = p.x;
int y = p.y;
if(r)
{
for(int i = 0; i < 8; i++)
if(a[x + directiix[i]][y + directiiy[i]] != 'X' && b[x + directiix[i]][y + directiiy[i]] == 0)
adaugare(x + directiix[i], y + directiiy[i], b[x][y] + 1, 1);
}
else
{
for(int i = 0; i < 8; i++)
if(a[x + directiix[i]][y + directiiy[i]] != 'X' && c[x + directiix[i]][y + directiiy[i]] == 0)
adaugare(x + directiix[i], y + directiiy[i], c[x][y] + 1, 0);
}
coada.pop();
}
}
int main()
{
ka >> n >> m;
ka.get();
for(int i = 1; i <= n; i++)
{
ka.getline(a[i],101);
for(int j = strlen(a[i]); j < m; j++)
a[i][j] = ' ';
for(int j = 0; j < m; j++)
{
if(a[i][j] == 'R')
{
romeo.x = i;
romeo.y = j;
}
else if(a[i][j] == 'J')
{
julieta.x = i;
julieta.y = j;
}
}
}
lee(romeo, 1);
lee(julieta, 0);
for(int i = 1; i <= n; i++)
for(int j = 0; j < m; j++)
{
if(b[i][j] == c[i][j] && b[i][j] && b[i][j] < minim)
{
minim = b[i][j];
raspuns.x = i;
raspuns.y = j;
}
}
ki << minim << " " << raspuns.x << " " << raspuns.y +1;
}