Cod sursa(job #2191611)

Utilizator lucametehauDart Monkey lucametehau Data 3 aprilie 2018 10:34:15
Problema Castel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <queue>

using namespace std;

ifstream cin ("castel.in");
ofstream cout ("castel.out");

const int nmax = 150;

struct Point {
  int x, y;
};

int n, m, l, c;
int a, b;
int sol;
bool ok;

int dx[] = {-1, 0, 1, 0};
int dy[] = { 0, 1, 0,-1};
Point q[1 + nmax * nmax];
bool f[1 + nmax * nmax];
int v[1 + nmax][1 + nmax];

int main() {
  cin >> n >> m >> c;
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
      cin >> v[i][j];
  f[c] = 1;
  b = c % m;
  a = (c - b) / m + 1;
  if(c % m == 0) {
    b = m;
    a--;
  }
  q[++l] = {a, b};
  ok = 1;
  while(ok) {
    ok = 0;
    for(int i = 1; i <= l; i++) {
      for(int dir = 0; dir < 4; dir++) {
        Point p = {q[i].x + dx[dir], q[i].y + dy[dir]};
        if(m * (p.x - 1) + p.y < 1 || m * (p.x - 1) + p.y > n * m)
          continue;
        if(f[v[p.x][p.y]] && !f[m * (p.x - 1) + p.y]) {
          f[m * (p.x - 1) + p.y] = 1;
          q[++l] = p;
          ok = 1;
        }
      }
    }
  }
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
      sol += f[m * (i - 1) + j];
  cout << sol;
  return 0;
}