Cod sursa(job #2236040)

Utilizator DandeacDan Deac Dandeac Data 27 august 2018 22:05:52
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.99 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");

int M,N,xr,xj,yr,yj,x,y;
int CX,CY;
vector <string> rj;
queue <int> qx,qy;
short romeo[101][101];
short juliet[101][101];

const int dx[] = {-1,-1,-1,0,0,1,1,1};
const int dy[] = {-1,0,1,-1,1,-1,0,1};

bool onMap(int x, int y)
{
    return (0<=x && x<N &&
            0<=y && y<M);
}

void lee(int p1,int p2, short dist[][101])
{
    qx.push(p1);
    qy.push(p2);

    while(!qx.empty())
    {
        x = qx.front();
        y = qy.front();
        qx.pop();
        qy.pop();

        for(int i=0;i<8;i++)
        {
            CX = x + dx[i];
            CY = y + dy[i];
            if(!onMap(CX,CY) || rj[CX][CY] == 'X')
                continue;
            if(dist[CX][CY] == 0)
            {
                dist[CX][CY] = dist[x][y] + 1;
                qx.push(CX);
                qy.push(CY);
            }
        }

    }

}
int main()
{
    f>>N>>M;
    rj.resize(N);

    string trash;
    getline(f, trash);

    for(int i=0;i<N;i++)
    {
        getline(f, rj[i]);
    }
    for(int i=0;i<N;i++)
    {
       for(int j=0;j<M;j++)
       {
            if(rj[i][j] == 'J')
            {
                juliet[i][j] = 1;
                xj = i;
                yj = j;
            }
            if(rj[i][j] == 'R')
            {
                romeo[i][j] = 1;
                xr = i;
                yr = j;
            }
       }

    }

    lee(xr,yr,romeo);
    lee(xj,yj,juliet);

    int costmin = 1000000,xmin =0,ymin = 0;
    for(int i = 0;i< N;i++)
    {
        for(int j =0;j<M; j++)
        {
            if(romeo[i][j]==juliet[i][j] && romeo[i][j] != 0 && costmin>romeo[i][j])
            {
                costmin = romeo[i][j];
                xmin = i+1;
                ymin = j+1;
            }
        }
    }
    g<<costmin<<' '<<xmin<<' '<<ymin;
    return 0;
}