Cod sursa(job #2601404)

Utilizator bem.andreiIceman bem.andrei Data 14 aprilie 2020 13:57:31
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <iostream>
#include <fstream>
#include<bits/stdc++.h>

using namespace std;
ifstream r("rj.in");
ofstream w("rj.out");
int n, m, a[102][102];
struct qu{
    int x, y;
};
qu nod;
queue<qu>q;
void citire(){
    r>>m>>n;
    r.get();
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n+1;j++){
            char c;
            c=r.get();
            if(c=='R' || c=='J'){
                nod.x=i;
                nod.y=j;
                q.push(nod);
                a[i][j]=1;
            }
            if(c=='X'){
                a[i][j]=-1;
            }
            if(c=='\n'){
                break;
            }
        }
    }
}
void solve(){
    while(q.size()!=0){
        int l=q.front().x, c=q.front().y;
        q.pop();
        if(l-1>0 && (a[l-1][c]==a[l][c] || a[l-1][c]==a[l][c]+1)){
            w<<a[l-1][c]-1<<" "<<l-1<<" "<<c;
            return;
        }
        if(c-1>0 && (a[l][c-1]==a[l][c] || a[l][c-1]==a[l][c]+1)){
            w<<a[l][c-1]-1<<" "<<l<<" "<<c-1;
            return;
        }
        if(l+1<=n && (a[l+1][c]==a[l][c] || a[l+1][c]==a[l][c]+1)){
            w<<a[l+1][c]-1<<" "<<l+1<<" "<<c;
            return;
        }
        if(c+1<=n && (a[l][c+1]==a[l][c] || a[l][c+1]==a[l][c]+1)){
            w<<a[l][c+1]-1<<" "<<l<<" "<<c+1;
            return;
        }
        if(l-1>0 && a[l-1][c]==0){
            a[l-1][c]=a[l][c]+1;
            nod.x=l-1;
            nod.y=c;
            q.push(nod);
        }
        if(c-1>0 && a[l][c-1]==0){
            a[l][c-1]=a[l][c]+1;
            nod.x=l;
            nod.y=c-1;
            q.push(nod);
        }
        if(l+1<=n && a[l+1][c]==0){
            a[l+1][c]=a[l][c]+1;
            nod.x=l+1;
            nod.y=c;
            q.push(nod);
        }
        if(c+1<=n && a[l][c+1]==0){
            a[l][c+1]=a[l][c]+1;
            nod.x=l;
            nod.y=c+1;
            q.push(nod);
        }
    }
}
int main()
{
    citire();
    solve();
    return 0;
}