Cod sursa(job #2664216)

Utilizator stanbianca611Stan Bianca stanbianca611 Data 28 octombrie 2020 10:20:31
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3 kb
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

ifstream f ("rj.in");
ofstream g ("rj.out");

int n, m, a[105][105], b[105][105], c[105][105];
deque <pair<int, int>> q1, q2;
int romeo_x, romeo_y, juliet_x, juliet_y;
int dl[]={-1, 0, 1, 0, -1, 1, -1, 1};
int dc[]={0, 1, 0, -1, -1, 1, 1, -1};

bool check(int x, int y)
{
    if(x>=1 && y>=1 && x<=n && y<=m && a[x][y]==0)
        return 1;
    return 0;
}


int main()
{
    f>>n>>m;
    char x;
    f.get(x);
    for(int i=1; i<=n; i++)
    {
        int ok=0;
        for(int j=1; j<=m; j++)
        {
            f.get(x);
            b[i][j]=100000;
            c[i][j]=100000;
            if(x=='X')
            {
                a[i][j]=-1;
            }
            else if (x=='R')
            {
                romeo_x=i;
                romeo_y=j;
                a[i][j]=0;
                b[i][j]=0;
                c[i][j]=0;
            }
            else if (x=='J')
            {
                juliet_x=i;
                juliet_y=j;
                a[i][j]=0;
                b[i][j]=0;
                c[i][j]=0;
            }
            else if (x==' ')
            {
                a[i][j]=0;
            }
            else if (x=='\n')
            {
                while(j<=m)
                {
                    ok=1;
                    a[i][j]=0;
                    j++;
                }
            }
        }
        if(ok==0) f.get(x);
    }
    q1.push_back(make_pair(romeo_x, romeo_y));
    q2.push_back(make_pair(juliet_x, juliet_y));

    pair <int, int> curr, next;
    while(!q1.empty())
    {
        curr=q1.front();
        q1.pop_front();
        for(int i=0; i<8; i++)
        {
            next.first=curr.first+dl[i];
            next.second=curr.second+dc[i];
            if(check(next.first, next.second) && b[next.first][next.second]>b[curr.first][curr.second]+1)
            {
                    b[next.first][next.second]=b[curr.first][curr.second]+1;
                    q1.push_back(make_pair(next.first, next.second));
            }
        }
    }
    while(!q2.empty())
    {
        curr=q2.front();
        q2.pop_front();
        for(int i=0; i<8; i++)
        {
            next.first=curr.first+dl[i];
            next.second=curr.second+dc[i];
            if(check(next.first, next.second) && c[next.first][next.second]>c[curr.first][curr.second]+1)
            {
                    c[next.first][next.second]=c[curr.first][curr.second]+1;
                    q2.push_back(make_pair(next.first, next.second));
            }
        }
    }
    int minn=10000005;
    int minn_x, minn_y;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(b[i][j]==c[i][j] && b[i][j]>0 && b[i][j]<minn)
            {
                minn=b[i][j];
                minn_x=i;
                minn_y=j;
            }
        }
    }
    g<<minn+1<<" "<<minn_x<<" "<<minn_y;
    return 0;
}