Cod sursa(job #1897181)

Utilizator verescodrutVeres Codrut Gica verescodrut Data 1 martie 2017 11:14:02
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.1 kb
#include <fstream>
#include <bits/stdc++.h>
#include <string.h>
#define pp pair<int,int>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int n,m,a[102][102],i,j,l,c,lnou,cnou,x,y,s=1;
int dl[]= {0,0,-1,1,-1,-1,1,1},dc[]= {-1,1,0,0,-1,1,-1,1};
char d[101];
queue <pp> q;
stack <pp> st;

void creare()
{
    while(!q.empty())
    {
        l=q.front().first;
        c=q.front().second;
        for(int i=0; i<4; i++)
        {
            int lnou= l+dl[i],cnou=c+dc[i];
            if(lnou==x&&cnou==y)a[x][y]=a[l][c]+1;
            if(a[lnou][cnou]==0 && lnou>=1 && lnou<=n && cnou>=1 && cnou<=m)
            {
                a[lnou][cnou]=a[l][c]+1;
                q.push(make_pair(lnou,cnou));

            }
        }
        q.pop();
    }
}
void afis()
{
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)g<<a[i][j]<<' ';
        g<<'\n';
    }
}
void creare2()
{
    while(!q.empty())
    {
        l=q.front().first;
        c=q.front().second;
        for(int i=0; i<4; i++)
        {
            int lnou= l+dl[i],cnou=c+dc[i];
            if(a[lnou][cnou]==a[l][c]-1 && lnou>=1 && lnou<=n && cnou>=1 && cnou<=m)
            {
                st.push(make_pair(lnou,cnou));
                q.push(make_pair(lnou,cnou));
                break;
            }
        }
        q.pop();
    }
}

int main()
{
    f>>n>>m;
    for(i=0; i<=n; i++)
    {
        f.getline(d,101);
        for(j=1; j<=m; j++)
        {
            if(d[j-1]=='X')a[i][j]=-1;
            else if(d[j-1]=='R')
            {
                q.push(make_pair(i,j));
            }
            else if(d[j-1]=='J')
            {
                a[i][j]=1;
                x=i;
                y=j;
            }
            else a[i][j]=0;
        }
    }
    creare();
    while(!q.empty())q.pop();
    q.push(make_pair(x,y));
    creare2();
    afis();
    while(!st.empty()&&a[x][y]/2>=s+1)
    {
        s++;
        st.pop();
    }
    g<<a[x][y]/2<<' '<<st.top().first<<' '<<st.top().second;
    f.close();
    g.close();
    return 0;
}