Pagini recente » Cod sursa (job #1754877) | Cod sursa (job #417636) | Cod sursa (job #2108630) | Cod sursa (job #373074) | Cod sursa (job #2634592)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int n, m, maxim, max_i, max_j, x, y, a, b, l, c, ox[] = {-1, 0, 1, 0, -1, -1, 1, 1}, oy[] = {0, 1, 0, -1, -1, 1, -1, 1};
queue < pair <int, int> > q;
string s;
struct specialMatrix{
int dist;
char persoana;
} mt[105][105];
void readInput() {
fin >> n >> m;
fin.get();
for (int i = 1; i <= n; ++i) {
getline(fin, s);
for (int j = 0; j < m; ++j) {
if (s[j] == 'X') {
mt[i][j + 1].dist = -1;
mt[i][j + 1].persoana = 'X';
}
else if (s[j] == 'R')
x = i, y = j + 1;
else if (s[j] == 'J')
a = i, b = j + 1;
}
}
return;
}
/*void viewMatrix() {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j)
cout << mt[i][j].dist << " ";
cout << "\n";
}
cout << "\n";
return;
}*/
void lee() {
bool stop = false;
while (!q.empty() && !stop) {
for (int i = 0; i < 8; ++i) {
if (q.front().first + ox[i] < 1 || q.front().first + ox[i] > n || q.front().second + oy[i] < 1 || q.front().second + oy[i] > m)
continue;
if (mt[q.front().first + ox[i]][q.front().second + oy[i]].dist == 0) {
mt[q.front().first + ox[i]][q.front().second + oy[i]].dist = mt[q.front().first][q.front().second].dist + 1;
if (mt[q.front().first + ox[i]][q.front().second + oy[i]].dist > maxim) {
maxim = mt[q.front().first + ox[i]][q.front().second + oy[i]].dist;
max_i = q.front().first + ox[i], max_j = q.front().second + oy[i];
}
mt[q.front().first + ox[i]][q.front().second + oy[i]].persoana = mt[q.front().first][q.front().second].persoana;
q.push(make_pair(q.front().first + ox[i], q.front().second + oy[i]));
}
else if (mt[q.front().first + ox[i]][q.front().second + oy[i]].dist != 0 && mt[q.front().first + ox[i]][q.front().second + oy[i]].dist == mt[q.front().first][q.front().second].dist + 1 &&
((mt[q.front().first + ox[i]][q.front().second + oy[i]].persoana == 'r' && mt[q.front().first][q.front().second].persoana == 'j') ||
(mt[q.front().first + ox[i]][q.front().second + oy[i]].persoana == 'j' && mt[q.front().first][q.front().second].persoana == 'r'))) {
l = q.front().first + ox[i], c = q.front().second + oy[i];
stop = true;
break;
}
}
q.pop();
//viewMatrix();
}
return;
}
int main() {
readInput();
q.push(make_pair(x, y));
q.push(make_pair(a, b));
mt[x][y].dist = 1, mt[a][b].dist = 1, mt[x][y].persoana = 'r', mt[a][b].persoana = 'j';
lee();
if (l != 0)
fout << mt[l][c].dist << " " << l << " " << c;
else
fout << maxim << " " << max_i << " " << max_j;
return 0;
}