Cod sursa(job #2162592)

Utilizator cella.florescuCella Florescu cella.florescu Data 12 martie 2018 12:03:11
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

const int MAXN = 1e2;
const int DIR = 8;
const int INF = 0x3f3f3f3f;

char mat[MAXN + 2][MAXN + 2];
int rom[MAXN + 2][MAXN + 2], jul[MAXN + 2][MAXN + 2];
int dirl[] = {-1, -1, 0, 1, 1, 1, 0, -1}, dirc[] = {0, 1, 1, 1, 0, -1, -1, -1};
queue <pair <int, int>> q;

inline void bfs(int lin, int col, int marc[MAXN + 2][MAXN + 2]) {
  memset(marc, INF, sizeof(marc[0][0]) * (MAXN + 2) * (MAXN + 2));
  marc[lin][col] = 1;
  q.emplace(lin, col);
  while (q.empty() == false) {
    lin = q.front().first;
    col = q.front().second;
    q.pop();
    for (int d = 0; d < DIR; ++d) {
      int newl = lin + dirl[d], newc = col + dirc[d];
      if (mat[newl][newc] != 'X' && marc[newl][newc] == INF) {
        marc[newl][newc] = marc[lin][col] + 1;
        q.emplace(newl, newc);
      }
    }
  }
}

int main()
{
    int n, m, l, c, rl, rc, jl, jc;
    memset(mat, 'X', sizeof mat);
    FILE *fin = fopen("rj.in", "r");
    fscanf(fin, "%d%d\n", &n, &m);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        fscanf(fin, "%c", &mat[i][j]);
        if (mat[i][j] == 'R') {
          rl = i; rc = j;
          mat[i][j] = ' ';
        } else if (mat[i][j] == 'J') {
          jl = i; jc = j;
          mat[i][j] = ' ';
        }
      }
      fscanf(fin, "\n");
    }
    fclose(fin);
    bfs(rl, rc, rom);
    bfs(jl, jc, jul);
    l = c = 0;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (rom[i][j] == jul[i][j] && rom[i][j] < rom[l][c]) {
          l = i; c = j;
        }
    FILE *fout = fopen("rj.out", "w");
    fprintf(fout, "%d %d %d\n", rom[l][c], l, c);
    fclose(fout);
    return 0;
}