Cod sursa(job #2946303)

Utilizator iLaurianLaurian Iacob iLaurian Data 24 noiembrie 2022 18:44:44
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.1 kb
// https://infoarena.ro/problema/rj
// OJI 2004, clasa a 10-a RJ
#include<fstream>
#include<cstring>
std::ifstream f ("rj.in");
std::ofstream g ("rj.out");
const int addRow[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int addCol[] = {1, 0, -1, 0, -1, 1,  1,-1};
char c;
int mat[101][101],i,j,m,n,inr,inj,sfr,sfj;
struct Pos {
    int row;
    int col;
};
Pos begr, begj;
Pos qr[101 * 101],qj[101 * 101];
void lee() {
    qr[0] = begr;
    qj[0] = begj;
    inr = inj = sfr = sfj = 0;
    mat[begr.row][begr.col] = 1;
    mat[begj.row][begj.col] = 1;
    Pos posr,posj;
    Pos nghr,nghj;
    while (inr <= sfr && inj <= sfj && posj.col!=posr.col && posj.row!=posr.row) {
        posr = qr[inr++];
        posj = qj[inj++];
        for (int i = 0; i < 8; i++) {
            nghr.row = posr.row + addRow[i];
            nghr.col = posr.col + addCol[i];
            if (mat[nghr.row][nghr.col]==32) {
                mat[nghr.row][nghr.col] = mat[posr.row][posr.col] + 1;
                qr[++sfr] = nghr;
            }
        nghj.row = posj.row + addRow[i];
        nghj.col = posj.col + addCol[i];
            if (mat[nghj.row][nghj.col]==32) {
                mat[nghj.row][nghj.col] = mat[posj.row][posj.col] + 1;
                qj[++sfj] = nghj;
        }
        }
    }
    g<< inr << " " << qr[inr].row << " " << qr[inr].col <<'\n';
}
int main()
{
    f>>n>>m;
    f.get();
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {f.get(c);
        mat[i][j] += c;
        if(c=='R')
        {
            begr.row = i;
            begr.col = j;
        }
        else
        if(c=='J')
        {
            begj.row = i;
            begj.col = j;
        }
        }
        f.get();
    }
    for (int i = 0; i <= m + 1; i++)
    mat[i][0] = mat[i][n + 1] = 88;
    for (int j = 0; j <= n + 1; j++)
    mat[0][j] = mat[m + 1][j] = 88;

    lee();

    //g<< posr.row << " " << posr.col<<'\n';
    g<<'\n';
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        g<<mat[i][j]<<" ";
        g<<'\n';
    }
    f.close();
    g.close();
    return 0;
}