Cod sursa(job #2337519)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 6 februarie 2019 15:06:12
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.1 kb
#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, 0, 1, 0, -1};
bool in_mat(int x, int y);
void solve()
{
    afisare(r);
    afisare(ju);
    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)
                if(r[i][j] < minn)
                {
                    minn = r[i][j];
                    solx = i;
                    soly = j;
                }
    fout << minn-1 << ' ' << 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;
            }
    }
}