Cod sursa(job #1806910)

Utilizator LaurIleIle Laurentiu Daniel LaurIle Data 15 noiembrie 2016 20:27:05
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

int ro[105][105], ju[105][105];
char s[105];
int ri, rj, ji, jj, n, m, tmin=1000000000, mi, mj;
const int dx[]={-1, -1, 0, 1, 1, 1, 0, -1};
const int dy[]={0, 1, 1, 1, 0, -1, -1, -1};
queue <pair <int, int> > q;

void read()
{
    ifstream f("rj.in");
    f >> n >> m; f.get();
    for(int i=1; i<=m; ++i)
    {
        f.getline(s,105,'\n');
        for(int j=0; j<m; ++j)
        {
            if(s[j]=='R')
            {
                ro[i][j+1]=1;
                ri=i; rj=j+1;
            }
            else
            if(s[j]=='J')
            {
                ju[i][j+1]=1;
                ji=i; jj=j+1;
            }
            else
            if(s[j]=='X')
            {
                ro[i][j+1]=-1;
                ju[i][j+1]=-1;
            }
        }
    }

}

void lee(int ii, int jj, int mat[105][105])
{
    q.push(make_pair(ii,jj)); pair <int, int> xx, yy;
    for(int i=0; i<=n+1; ++i)
    {mat[i][0]=-1; mat[i][n+1]=-1;}
    for(int i=0; i<=m+1; ++i)
    {mat[0][i]=-1; mat[m+1][i]=-1;}
    while(!q.empty())
    {
        xx=q.front(); q.pop();
        for(int i=0; i<8; ++i)
        {
            yy.first = xx.first + dx[i];
            yy.second = xx.second + dy[i];
            if(mat[yy.first][yy.second] == 0)
            {
                mat[yy.first][yy.second] = mat[xx.first][xx.second] +1;
                q.push(make_pair(yy.first,yy.second));
            }
        }
    }
}

void determ()
{
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            if(ro[i][j]==ju[i][j] && (ro[i][j]!=-1 && ju[i][j]!=-1) &&(ro[i][j]!=0 && ju[i][j]!=0)  && ro[i][j]<tmin)
            {
                if(tmin>ro[i][j])
                {
                    tmin=ro[i][j];
                    mi=i; mj=j;
                }
            }
        }
    }
}

void out()
{
    ofstream g("rj.out");
    g << ro[mi][mj] << ' ' << mi << ' ' << mj;
    g.close();
}

int main()
{
    read();
    lee(ri, rj, ro);
    lee(ji, jj, ju);
    determ();
    out();
    return 0;
}