Pagini recente » Cod sursa (job #2339626) | Cod sursa (job #3139469)
#include <fstream>
using namespace std;
ifstream cin("rj.in");
ofstream cout("rj.out");
int st = 1, dr = 0;
pair<int, int> coada[180 * 180];
int dirx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int diry[] = {-1, 1, 0, 0, 1, -1, -1, 1};
int matr[105][105], matj[105][105];
int n, m;
void push(int l, int c )
{
coada[++dr].first = l;
coada[dr].second = c;
}
void pop()
{
++st;
}
pair <int, int> front()
{
return coada[st];
}
bool empty()
{
return st > dr;
}
void lee(int mat[105][105], int l1, int c1)
{
st= 1;
dr = 0;
push(l1, c1);
mat[l1][c1] = 1;
int i;
for ( i = 0; i <= m + 1; ++i )
{
mat[0][i] = -1;
mat[n + 1][i] = -1;
}
for ( i = 0; i <= n + 1; ++i )
{
mat[i][m + 1] = -1;
mat[i][0] = -1;
}
while ( empty() == false )
{
int x = front().first, y = front().second;
pop();
for ( i = 0; i < 8; ++i )
{
int new_x = x + dirx[i];
int new_y = y + diry[i];
if ( mat[new_x][new_y] == 0)
{
mat[new_x][new_y] = mat[x][y] + 1;
push(new_x, new_y);
}
}
}
}
int main()
{
cin >> n >> m;
int i, j, l1, l2, c1, c2;
char s[101];
cin.get();
for ( i = 1; i <= n; ++i )
{
cin.getline(s, 100);
for ( j = 0; j < m; ++j)
{
if ( s[j] == 'R' )
l1 = i, c1 = j + 1;
if ( s[j] == 'J' )
l2 = i, c2 = j + 1;
if ( s[j] == 'X' )
matr[i][j + 1] = matj[i][j + 1] = -1;
}
}
lee(matr, l1, c1);
lee(matj, l2, c2);
int rasp = n * m + 1, x1, y1;
for ( i = 1; i <= n; ++i )
for ( j = 1; j <= m; ++j )
{
if ( matr[i][j] == matj[i][j] && matr[i][j] > 0 )
{
if ( rasp > matr[i][j] )
{
rasp = matr[i][j];
x1 = i;
y1 = j;
}
}
}
/*for ( i = 1; i <= n; ++i )
{
for ( j = 1; j <= m; ++j )
cout << matr[i][j] << " ";
cout << endl;
}*/
cout << rasp << " " << x1 << " " << y1 << '\n';
return 0;
}