Pagini recente » fmi-no-stress-2012/clasament | Cod sursa (job #839780) | Cod sursa (job #1025891) | Cod sursa (job #2860217) | Cod sursa (job #2664949)
// Rj.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
struct Pos {
int lin, col;
};
int di[8] = { -1,-1,-1,0,0,1,1,1 };
int dj[8] = { -1,0,1,-1,1,-1,0,1 };
int A_R[105][105], A_J[105][105], N, M;
struct Pos RomeoPos, JulietaPos, Intersection;
int minim = 100000;
void Scanf()
{
fin >> N >> M;
fin.get();
for (int lin = 1; lin <= N; lin++)
{
char s[110];
fin.getline(s, 110);
for (int col = 0; col < M; col++)
{
if (s[col] == 'R')
{
RomeoPos.lin = lin;
RomeoPos.col = col + 1;
A_R[lin][col + 1] = 1;
A_J[lin][col + 1] = -1;
}
else if (s[col] == 'J')
{
JulietaPos.lin = lin;
JulietaPos.col = col + 1;
A_R[lin][col + 1] = -1;
A_J[lin][col + 1] = 1;
}
else if (s[col] == 'X')
{
A_R[lin][col + 1] = -1;
A_J[lin][col + 1] = -1;
}
else
{
A_R[lin][col + 1] = 0;
A_J[lin][col + 1] = 0;
}
}
}
}
void Lee(int A[][105], struct Pos start)
{
queue <pair<int, int>> Q;
Q.push(make_pair(start.lin, start.col));
while (!Q.empty())
{
int i = Q.front().first, j = Q.front().second;
for (int k = 0; k < 8; k++)
{
int iv = i + di[k], jv = j + dj[k];
if (iv >= 1 && iv <= N && jv >= 1 && jv <= M && A[iv][jv] == 0)
{
A[iv][jv] = A[i][j] + 1;
Q.push(make_pair(iv, jv));
}
}
Q.pop();
}
}
void MinIntersction(struct Pos& Intersection)
{
for (int lin = 1; lin <= N; lin++)
for (int col = 1; col <= M; col++)
{
if (A_R[lin][col] == A_J[lin][col] && A_R[lin][col] > 0 && A_J[lin][col] < minim)
{
minim = A_R[lin][col];
Intersection.lin = lin;
Intersection.col = col;
}
}
}
void Printf() {
fout << minim << " " << Intersection.lin << " " << Intersection.col;
}
int main()
{
Scanf();
Lee(A_R, RomeoPos);
Lee(A_J, JulietaPos);
MinIntersction(Intersection);
Printf();
return 0;
}