Cod sursa(job #2124490)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 7 februarie 2018 11:40:19
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 205;

int a[NMAX][NMAX];
int p, n, m, xc, yc, k, g;

int dx1[] = { 0, 1, 0, -1,  0};
int dy1[] = { 0, 0, 1,  0, -1};

int dx2[] = { 0, 1, 0, -1,  0, 0, 1, 2,  1,  0, -1, -2, -1};
int dy2[] = { 0, 0, 1,  0, -1, 2, 1, 0, -1, -2, -1,  0,  1};

const int INF = 0x3f3f3f3f;

struct galerie {
  int x1, y1, x2, y2;
}gal[101];

void fillFox(int x, int y, int z) {
  switch(z) {
    case 0:
      a[x][y] = INF;
      return;
    case 1:
      for (int d = 0; d < 5; d++)
        a[x + dx1[d]][y + dy1[d]] = INF;
      return;
    case 2:
      for (int d = 0; d < 13 ; d++)
        a[x + dx2[d]][y + dy2[d]] = INF;
      return;
  }
}

void read() {
  scanf("%d %d %d %d %d %d ", &p, &n, &m, &xc, &yc, &k);
  for (int i = 1; i <= k; i++) {
    int x, y, z;
    scanf("%d %d %d ", &x, &y, &z);
    fillFox(x, y, z);
  }
  scanf("%d ", &g);
  for (int i = 1; i <= g; i++) {
    scanf("%d %d %d %d ", &gal[i].x1, &gal[i].y1, &gal[i].x2, &gal[i].y2);
    a[gal[i].x1][gal[i].y1] = i;
    a[gal[i].x2][gal[i].y2] = i;
  }
}

void bordare() {
  for (int i = 0; i <= n + 1; i++) {
    a[i][0] = a[i][m + 1] = INF;
  }
  for (int j = 0; j <= n + 1; j++) {
    a[0][j] = a[n + 1][j] = INF;
  }
}

void solve1() {
  queue<pair<int, int>> q;
  q.push({ xc, yc });

  int xf, yf, lg;
  a[xc][yc] = -1;

  while(!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();

    for (int d = 1; d < 5; d++) {
      int nx = x + dx1[d], ny = y + dy1[d];
      if (a[nx][ny] == INF) continue;

      if (a[nx][ny] > 0) { // gasit
        lg = -a[x][y];
        xf = nx;
        yf = ny;
        break;
      } else if(!a[nx][ny]){
        a[nx][ny] = a[x][y] - 1;
        q.push({ nx, ny });
      }
    }
  }

  printf("%d %d %d ", xf, yf, lg);
}

void solve2() {

}

int main() {
  freopen("cartite.in", "r", stdin);
  freopen("cartite.out", "w", stdout);

  read();
  bordare();

  if (p == 1)
    solve1();
  else solve2();

  return 0;
}