Cod sursa(job #2958433)

Utilizator LukyenDracea Lucian Lukyen Data 26 decembrie 2022 16:32:17
Problema Barbar Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.55 kb
#include <fstream>
#include <queue>
#include <climits>
#include <cstring>
using namespace std;

ifstream fin("barbar.in");
ofstream fout("barbar.out");
const int vec_len = 1005;

struct pos
{
  int l;
  int c;
};

int n, m;
int dmap[vec_len][vec_len];
bool vis[vec_len][vec_len];
int dirl[] = {-1, 0, 1, 0}, dirc[] = {0, 1, 0, -1};

pos tstart, texit;
queue<pos> drag;
queue<pos> path;

bool check(int l, int c)
{
  if (l < 1 || l > n)
    return false;

  if (c < 1 || c > m)
    return false;

  if (dmap[l][c] == -1)
    return false;

  return true;
}

void dragLee()
{
  while (!drag.empty())
  {
    pos curr = drag.front();
    vis[curr.l][curr.c] = true;
    drag.pop();

    int nl = curr.l, nc = curr.c;
    for (int i = 0; i < 4; i++)
    {
      nl = curr.l + dirl[i];
      nc = curr.c + dirc[i];

      if (check(nl, nc) && !vis[nl][nc])
      {
        dmap[nl][nc] = min(dmap[nl][nc], dmap[curr.l][curr.c] + 1);
        drag.push(pos{nl, nc});
      }
    }
  }
}

bool pathLee(int val)
{
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      vis[i][j] = false;

  while (!path.empty())
    path.pop();

  path.push(pos{tstart.l, tstart.c});
  while (!path.empty())
  {
    pos curr = path.front();
    if (curr.l == texit.l && curr.c == texit.c)
      return true;
    path.pop();

    int nl, nc;
    for (int i = 0; i < 4; i++)
    {
      nl = curr.l + dirl[i];
      nc = curr.c + dirc[i];

      if (check(nl, nc) && !vis[nl][nc] && dmap[nl][nc] >= val)
      {
        path.push(pos{nl, nc});
        vis[nl][nc] = true;
      }
    }
  }

  return false;
}

int main()
{
  fin.tie(NULL);
  fout.tie(NULL);
  ios::sync_with_stdio(false);

  fin >> n >> m;

  char x;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
    {
      fin >> x;
      dmap[i][j] = INT_MAX;
      if (x == 'D')
        drag.push(pos{i, j}), dmap[i][j] = 0;
      else if (x == 'I')
        tstart = pos{i, j}, dmap[i][j] = INT_MAX;
      else if (x == '*')
        dmap[i][j] = -1;
      else if (x == 'O')
        texit = pos{i, j}, dmap[i][j] = INT_MAX;
    }

  dragLee();

  if (dmap[tstart.l][tstart.c] == INT_MAX)
  {
    fout << -1;
    return 0;
  }

  int left = 1, right = n * m + 1;
  while (left < right)
  {
    int mid = (left + right) / 2;
    bool res = pathLee(mid);

    if (!res)
      right = mid;
    else
      left = mid + 1;
  }

  if (pathLee(right))
    fout << right;
  else
    fout << right - 1;

  return 0;
}