Cod sursa(job #2990515)

Utilizator MemeComan Mihai Matei Meme Data 7 martie 2023 23:32:04
Problema Barbar Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <bits/stdc++.h>

using namespace std;
const int N = 1002;
const int dl[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
int b[N][N];
int f[N][N];
struct c{ // coordonate
  int l, c;
};
int ff = 0;
int lf, cf;
bool fiil(int n, int l, int c){
  if( l == lf && c == cf) // daca am ajuns la iesire
    return true;
   f[l][c] = ff;
  for(int k = 0; k < 4; k++){
    int cl = l + dl[k];
    int cc = c + dc[k];
    if(b[cl][cc] >= n && f[cl][cc] != ff){
      if(fiil(n, cl, cc) == true)
        return true;
    }
  }
  return false;
}
int main()
{
    ifstream in("barbar.in");
    ofstream out("barbar.out");
    queue <c> q;
    int n , m;
    in >> n >> m;
    int li, ci;
    char x;
    for(int i = 1; i <= n; i++){
      for(int j = 1; j <= m; j++){
        in >> x;
        switch(x){
          case '.':
            b[i][j] = -1;
            break;
          case '*':
            b[i][j] = -2;
          break;
          case 'D':
            b[i][j] = 0;
            q.push((c){i,j});
          break;
          case 'I':
            b[i][j] = -1;
            li = i; //Inceput
            ci = j;
            break;
          case 'O':
            b[i][j] = -1;
            lf = i; //Final
            cf = j;
            break;
        }
      }
    }
    while(!q.empty()){
      c x = q.front();
      q.pop();
      for(int k = 0; k < 4; k++){
        c y;
        y.l = x.l + dl[k];
        y.c = x.c + dc[k];
        if(b[y.l][y.c] == -1){
          b[y.l][y.c] = b[x.l][x.c] + 1;
          q.push((c){y.l,y.c});
        }
      }
    }
    /*for(int i = 1; i <= n; i++){
      for(int j = 1; j <= m; j ++){
        cout << b[i][j] << " ";
      }
      cout << '\n';
    }*/
    int st = 1, dr = b[li][ci];
    int rez = -1;
    while(st <= dr){
      int mij = (st + dr) / 2;
      ff++;
      if(fiil(mij, li, ci)){
        rez = mij;
        st = mij + 1;
      }
      else
        dr = mij - 1;
    }
    out << rez;
    return 0;
}