Cod sursa(job #2193564)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 10 aprilie 2018 15:39:56
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.64 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");
int n,i,j,m,r[102][102],ju[102][102],lin,col,tmin,x,y,xx,yy;
char c;
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
queue<int>roml;
queue<int>romc;
queue<int>jull;
queue<int>julc;
int main()
{
    f >> n >> m;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            f.get(c);
            if (c == 'R')
            {
                roml.push(i);
                romc.push(j);
                r[i][j] = 1;
            }
            else if (c == 'J')
            {
                jull.push(i);
                julc.push(j);
                ju[i][j] = 1;
            }
            else if (c == ' ')
            {
                ju[i][j] = r[i][j] = 0;
            }
            else if (c == 'X')
            {
                ju[i][j] = r[i][j] = -1;
            }
            else if (c=='\n')
            {
                ju[i][j] = r[i][j] = 0;
            }
        }
        if (c!='\n') f.get(c);
    }
    for (i = 0; i <= n+1; i++)
    {
        r[i][0] = r[i][m+1] = ju[i][0] = ju[i][m+1] = -1;
    }
    for (i = 0; i <= m+1; i++)
    {
        r[0][i] = r[n+1][i] = ju[0][i] = ju[n+1][i] = -1;
    }
    r[roml.front()][romc.front()] = 1;
    ju[jull.front()][julc.front()] = 1;
    while (!roml.empty())
    {
        x = roml.front();
        y = romc.front();
        for (i = 0; i < 8; i++)
        {
            xx = x + dx[i];
            yy = y + dy[i];
            if (r[xx][yy] == 0)
            {
                roml.push(xx);
                romc.push(yy);
                r[xx][yy] = r[x][y] + 1;
            }
        }
        roml.pop();
        romc.pop();
    }
    while (!jull.empty())
    {
        x = jull.front();
        y = julc.front();
        for (i = 0; i < 8; i++)
        {
            xx = x + dx[i];
            yy = y + dy[i];
            if (ju[xx][yy] == 0)
            {
                jull.push(xx);
                julc.push(yy);
                ju[xx][yy] = ju[x][y] + 1;
            }
        }
        jull.pop();
        julc.pop();
    }
    tmin = 2000000000;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if (r[i][j] < tmin && r[i][j] != 0 && r[i][j] != -1)
            {
                if (ju[i][j] == r[i][j])
                {
                    tmin = ju[i][j];
                    lin = i;
                    col = j;
                }
            }
        }
    }
    g << tmin << " " << lin << " " << col << '\n';
    return 0;
}