Cod sursa(job #1928602)

Utilizator Cristi_ChiraChira Cristian Cristi_Chira Data 16 martie 2017 15:51:33
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <climits>
#include <fstream>
#include <queue>
#include <string>
#include <cstring>
#define x first
#define y second
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
int pr[105][105], pj[105][105], n, m, li[]={0, 1, 0, -1, 1, -1, 1, -1}, lo[]={1, 0, -1, 0, 1, -1, -1, 1};
char RJ[104];
queue < pair<int, int> > q;
pair<int, int> poz, npoz;
bool verif1(int x, int y)
{
    if(x<=n && x>=1 && y<=m && y>=1 && pr[x][y]!='X' && pr[x][y]==0)
    return 1;
    return 0;
}
bool verif2(int x, int y)
{
    if(x<=n && x>=1 && y<=m && y>=1 && pj[x][y]!='X' && pj[x][y]==0)
    return 1;
    return 0;
}
void lee1(int x, int y)
{
    q.push(make_pair(x,y));
    pr[x][y]=1;
    while(!q.empty())
    {
        poz=q.front();
        q.pop();
        for(int i=0; i<8; i++)
        {
            npoz.x=poz.x+li[i];
            npoz.y=poz.y+lo[i];
            if(verif1(npoz.x, npoz.y))
            {
                q.push(npoz);
                pr[npoz.x][npoz.y]=pr[poz.x][poz.y]+1;
            }
        }
    }
}
void lee2(int x, int y)
{
    q.push(make_pair(x,y));
    pj[x][y]=1;
    while(!q.empty())
    {
        poz=q.front();
        q.pop();
        for(int i=0; i<8; i++)
        {
            npoz.x=poz.x+li[i];
            npoz.y=poz.y+lo[i];
            if(verif2(npoz.x, npoz.y))
            {
                q.push(npoz);
                pj[npoz.x][npoz.y]=pj[poz.x][poz.y]+1;
            }
        }
    }
}
int minim=INT_MAX;
pair<int, int> r;
pair<int, int> h;
int main()
{
    fin>>n>>m;
    fin.get();
    for(int i=1; i<=n; i++)
    {
        fin.getline(RJ, m+1);
        for(int j=0; j<m; j++)
        {
            if(RJ[j]=='R')
            {
                r.x=i;
                r.y=j+1;

            }
            else if(RJ[j]=='J')
            {
                h.x=i;
                h.y=j+1;

            }
            else if(RJ[j]=='X')
            {
                pr[i][j+1]=-1;
                pj[i][j+1]=-1;
            }
        }
    }
    lee1(r.x, r.y);
    lee2(h.x, h.y);
    pair<int, int> finall;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
                if(pr[i][j]==pj[i][j] && pr[i][j]<minim && pr[i][j]!=-1 && pr[i][j]!=0)
                    minim=pj[i][j], finall.x=i, finall.y=j;
        }
    }
    fout<<minim<<" "<<finall.x<<" "<<finall.y;
    return 0;
}