Pagini recente » Cod sursa (job #1105061) | Cod sursa (job #388366) | Cod sursa (job #2694444) | Cod sursa (job #510531) | Cod sursa (job #2634576)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int n, m, 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;
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();
fout << mt[l][c].dist << " " << l << " " << c;
return 0;
}