Pagini recente » Cod sursa (job #1745072) | Cod sursa (job #520198) | Cod sursa (job #1423206) | Cod sursa (job #2487291) | Cod sursa (job #2337528)
#include <bits/stdc++.h>
#define NM 105
using namespace std;
ifstream fin ("rj.in");
ofstream fout ("rj.out");
struct poz
{
int x, y;
};
int n, m, r[105][105], ju[105][105];
int rx, ry, jx, jy;
void read();
void afisare(int x[105][105])
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
fout << x[i][j] << ' ';
fout << '\n';
}
fout << '\n';
}
const int dx[]= {0, 0, 1, 1, 1, -1, -1, -1};
const int dy[]= {1, -1, 0, -1, 1, 1, 0, -1};
bool in_mat(int x, int y);
void solve()
{
int minn = INT_MAX, solx, soly;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(r[i][j] == ju[i][j] && r[i][j]!=-1 && r[i][j])
if(r[i][j] < minn)
{
minn = r[i][j];
solx = i;
soly = j;
}
fout << minn << ' ' << solx << ' ' << soly << '\n';
}
void lee(int xs, int ys, int a[105][105])
{
queue<poz> q;
q.push({xs, ys});
a[xs][ys] = 1;
while(!q.empty())
{
poz cur = q.front();
q.pop();
for(int i=0; i<8; i++)
{
int x = cur.x + dx[i];
int y = cur.y + dy[i];
if(in_mat(x, y) && a[x][y] == 0)
{
a[x][y] = a[cur.x][cur.y] + 1;
q.push({x, y});
}
}
}
}
int main()
{
read();
lee(rx, ry, r);
lee(jx, jy, ju);
solve();
return 0;
}
bool in_mat(int x, int y)
{
if(x<1 or y<1 or x>n or y>m)
return 0;
return 1;
}
void read()
{
fin >> n >> m;
char s[NM];
fin.get();
char c;
for(int i=1; i<=n; i++)
{
fin.getline(s+1, NM);
for(int j=1; j<=m; j++)
if(s[j] == 'X')
r[i][j] = ju[i][j] = -1;
else if(s[j] == 'R')
{
rx = i;
ry = j;
}
else if(s[j] == 'J')
{
jx = i;
jy = j;
}
}
}