Pagini recente » Cod sursa (job #2528969) | Cod sursa (job #2257524) | Cod sursa (job #2259670) | Cod sursa (job #1712798) | Cod sursa (job #2225456)
#include <queue>
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
istream & in = fin;
ostream & out = fout;
struct vec2{
int x, y;
vec2()
{
}
vec2(int x, int y) : x(x), y(y)
{
}
bool in_bounds(vec2 vec)
{
return (this->x >= 0 && this->y >= 0 && this->x < vec.x && this->y < vec.y);
}
};
vec2 size;
vec2 r, j;
int rmat[114][114], jmat[114][114];
vec2 dir[8] = {vec2(-1, -1), vec2(0, -1), vec2(1, -1), vec2(1, 0), vec2(1, 1), vec2(0, 1), vec2(-1, 1), vec2(-1, 0)};
vec2 pos;
int dist = -1;
int conv(char c)
{
if(c == 'R' || c == 'J'){
return 1;
}else if(c == ' '){
return 0;
}else if(c == 'X'){
return -1;
}
}
void read()
{
in >> size.y >> size.x;
in.get();
char c;
for(int y = 0; y < size.y; y++){
for(int x = 0; x < size.x; x++){
c = in.get();
if(c == 'R'){
r = vec2(x, y);
}else if(c == 'J'){
j = vec2(x, y);
}
rmat[x][y] = jmat[x][y] = conv(c);
}
while(in.get() != '\n' && !in.eof());
}
rmat[j.x][j.y] = jmat[r.x][r.y] = 0;
}
void dunno(vec2 start, int mat[114][114])
{
queue<vec2> qu;
qu.push(start);
vec2 curr, neckst;
while(!qu.empty()){
curr = qu.front();
qu.pop();
for(int i = 0; i < 8; i++){
neckst = curr;
neckst.x += dir[i].x, neckst.y += dir[i].y;
if(mat[neckst.x][neckst.y] == 0 && neckst.in_bounds(size)){
mat[neckst.x][neckst.y] = mat[curr.x][curr.y] + 1;
qu.push(neckst);
}
}
}
}
void findy()
{
for(int y = 0; y < size.y; y++){
for(int x = 0; x < size.x; x++){
if(rmat[x][y] == jmat[x][y] && rmat[x][y] > 0){
if(dist == -1){
dist = rmat[x][y];
}else{
dist = min(dist, rmat[x][y]);
}
pos = vec2(x, y);
}
}
}
}
void solve()
{
dunno(r, rmat);
dunno(j, jmat);
findy();
}
void write()
{
out << dist << " " << pos.y + 1 << " " << pos.x + 1;
}
int main()
{
read();
solve();
write();
}