Submission #1195984

#TimeUsernameProblemLanguageResultExecution timeMemory
1195984LucaLucaMTracks in the Snow (BOI13_tracks)C++20
47.50 / 100
2126 ms1033632 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

using ll = long long;
#define debug(x) #x << " = " << x << '\n'

const int NMAX = 4000;
const int dx[4] = {-1, 0, +1, 0};
const int dy[4] = {0, -1, 0, +1};

char a[NMAX + 2][NMAX + 2];
bool vis[NMAX + 2][NMAX + 2];

void dfs(int x, int y) {
  vis[x][y] = true;
  for (int k = 0; k < 4; k++) {
    int xx = x + dx[k];
    int yy = y + dy[k];
    if (!vis[xx][yy] && a[xx][yy] == a[x][y]) {
      dfs(xx, yy);
    }
  }
}

int main() {
  #ifdef LOCAL
    freopen("input.txt", "r", stdin);
  #endif
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
 
  int n, m;
  std::cin >> n >> m;
  
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      std::cin >> a[i][j];
    }
  }

  int pasite = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] != '.') {
        pasite++;
      }
    }
  }

  int answer = 0;

  while (true) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        vis[i][j] = false;
      }
    }
    char c = a[1][1];
    dfs(1, 1);
    answer++;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (vis[i][j]) {
          cnt++;
          if (c == 'F') {
            a[i][j] = 'R';
          } else {
            a[i][j] = 'F';
          }
        }   
      }
    }
    if (cnt == pasite) {
      break;
    }
  }

  std::cout << answer;
  
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...