Submission #725726

#TimeUsernameProblemLanguageResultExecution timeMemory
725726asdfgraceTracks in the Snow (BOI13_tracks)C++17
84.69 / 100
2103 ms45932 KiB
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << endl)
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << "  "); PRINT("}\n");)
using i64 = int64_t;
const int INF = 1000000007; //998244353

struct S {
  int n, m;
  vector<vector<char>> a;
  vector<vector<bool>> visited;

  void run() {
    cin >> n >> m;
    a.resize(n, vector<char>(m));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> a[i][j];
      }
    }
  }
  
  void bfs() {
    priority_queue<array<int, 3>> q; // {d, x, y}
    q.push({-1, 0, 0});
    visited.resize(n, vector<bool>(m, false));
    visited[0][0] = true;
    int ans = 1;
    while (!q.empty()) {
      int x = q.top()[1];
      int y = q.top()[2];
      int d = -q.top()[0];
      q.pop();
      ans = max(ans, d);
      PV(x); PV(y); PV(d); PRINT('\n');
      char c = a[x][y];
      if (ok(x, y + 1, c) >= 0) {
        q.push({-ok(x, y + 1, c) - d, x, y + 1});
        visited[x][y + 1] = true;
      }
      if (ok(x, y - 1, c) >= 0) {
        q.push({-ok(x, y - 1, c) - d, x, y - 1});
        visited[x][y - 1] = true;
      }
      if (ok(x + 1, y, c) >= 0) {
        q.push({-ok(x + 1, y, c) - d, x + 1, y});
        visited[x + 1][y] = true;
      }
      if (ok(x - 1, y, c) >= 0) {
        q.push({-ok(x - 1, y, c) - d, x - 1, y});
        visited[x - 1][y] = true;
      }
    }
    cout << ans << '\n';
  }
  
  int ok(int x, int y, char c) {
    if (x < 0 || x >= n) return -1;
    if (y < 0 || y >= m) return -1;
    if (visited[x][y]) return -1;
    if (a[x][y] == '.') return -1;
    return (a[x][y] == c ? 0 : 1);
  }
  
  
};

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  auto sol = make_unique<S>();
  sol->run();
  sol->bfs();
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...