Cod sursa(job #2332017)

Utilizator mihneazarojanuMihnea Bogdan Zarojanu mihneazarojanu Data 30 ianuarie 2019 11:55:43
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.26 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N=102;

const int dl[]={-1, 0, 1, 0, -1, -1, 1, 1};
const int dc[]={0, 1, 0, -1, -1, 1, -1, 1};

struct pct{
    int l, c;
};

pct q[N*N], s[N*N], x, y, f;

int a[N][N];

int main()
{
    int n, m;
    char c[106];
    in>>n>>m;
    for(int i=0; i<=n+1; i++){
        a[i][0]=a[i][m+1]=-2;
    }
    for(int j=0; j<=m+1; j++){
        a[0][j]=a[n+1][j]=-2;
    }
    int st=0, dr=-1, vf=-1;
    in.ignore();
    for(int i=1; i<=n; i++){
        in.getline(1+c, 106);
        for(int j=1; j<=m; j++){
            if(c[j]=='R'){
                a[i][j]=1;
                q[++dr]=(pct){i, j};
            }
            if(c[j]=='J'){
                f=(pct){i, j};
            }
            if(c[j]=='X'){
                a[i][j]=-1;
            }
            if(c[j]==' '){
                a[i][j]=0;
            }
        }
    }/*
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(a[i][j]>-1){
                cout<<" ";
            }
            cout<<a[i][j]<<" ";
        }
        cout<<"\n";
    }*/
    while(st<=dr && a[f.l][f.c]==0){
        x=q[st++];
        for(int i=0; i<8; i++){
            y.l=x.l+dl[i];
            y.c=x.c+dc[i];
            if(a[y.l][y.c]==0){
                q[++dr]=y;
                a[y.l][y.c]=1+a[x.l][x.c];
            }
        }
    }
    s[++vf]=f;
    int val=a[f.l][f.c]/2+a[f.l][f.c]%2;
    while(a[x.l][x.c]>val){
        for(int i=0; i<8; i++){
            y.l=x.l+dl[i];
            y.c=x.c+dc[i];
            if(a[y.l][y.c]==a[x.l][x.c]-1){
                s[++vf]=y;
                x=y;
                break;
            }
        }
        //cout<<a[s[vf].l][s[vf].c]<<"\n";
    }
    out<<a[s[vf].l][s[vf].c]<<" "<<s[vf].l<<" "<<s[vf].c;/*
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(a[i][j]==-1){
                cout<<"#";
            }else{
                if(a[i][j]==0){
                    cout<<"-";
                }else{
                    cout<<a[i][j];
                }
            }
            cout<<" ";
        }
        cout<<"\n";
    }*/
    return 0;
}