Cod sursa(job #2946363)

Utilizator iLaurianLaurian Iacob iLaurian Data 24 noiembrie 2022 19:47:10
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.06 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[102][102],i,j,m,n,inr,inj,sfr,sfj;
struct Pos {
    int row;
    int col;
};
Pos begr, begj;
Pos qr[102 * 102],qj[102 * 102];
Pos posr,posj;
Pos nghr,nghj;
void lee() {
    posr = qr[0] = begr;
    posj = qj[0] = begj;
    inr = inj = sfr = sfj = 0;
    mat[begr.row][begr.col] = 1;
    mat[begj.row][begj.col] = 1;
    do{
        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;
        }
        }
        posr = qr[inr++];
        posj = qj[inj++];
    }while (inr <= sfr && inj <= sfj && posj.col!=posr.col && posj.row!=posr.row);
    g<< inj << " " << qr[sfr].row+1 << " " << qr[sfr].col;
}
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<<'\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;
}