Cod sursa(job #2978451)

Utilizator davidenko22Stancu David-Andrei davidenko22 Data 13 februarie 2023 19:27:47
Problema Rj Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

const int MAX = 102;

int n, m, v[MAX][MAX], ro[MAX][MAX], ju[MAX][MAX], xr, yr, xj, yj;
string str;

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

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

bool inauntru(int x, int y){
    return ( (1 <= x && x <= n) && (1 <= y && y <= m) );
}

void lee(int x, int y, int vf[MAX][MAX]){
    deque<pair<int, int>> cd;
    cd.push_back({x, y});
    vf[x][y] = 1;
    while(!cd.empty()){
      auto [x, y] = cd.back();
      for(int k = 0; k < 8; k++){
        int xnou = x+dx[k];
        int ynou = y+dy[k];
        if(inauntru(xnou, ynou) && vf[xnou][ynou] == 0 && v[xnou][ynou] == 0){
          vf[xnou][ynou] = 1+vf[x][y];
          cd.push_front({xnou, ynou});
        }
      }
      cd.pop_back();
    }
}

int main()
{
    fin >> n >> m;
    fin.ignore();
    for(int i = 1; i <= n; i++){
      getline(fin, str);
      for(int j = 0; j < str.size(); j++){
        if(str[j] == 'R'){
          v[i][j+1] = 0;
          xr = i;
          yr = j+1;
        }
        else if(str[j] == 'J'){
          v[i][j+1] = 0;
          xj = i;
          yj = j+1;
        }
        else if(str[j] == ' '){
          v[i][j+1] = 0;
        }
        else{
          v[i][j+1] = 1;
        }
      }
    }
    lee(xr, yr, ro);
    lee(xj, yj, ju);
    int ans = 1000000, posx = 0, posy = 0;
    for(int i = 1; i <= n; i++){
      for(int j = 1; j <= m; j++){
        if(ro[i][j] == ju[i][j] && ro[i][j] != 0){
          if(ro[i][j] < ans){
            ans = ro[i][j];
            posx = i;
            posy = j;
          }
        }
      }
    }
    fout << ans << " " << posx << " " << posy;
    return 0;
}