Submission #817130

#TimeUsernameProblemLanguageResultExecution timeMemory
817130gun_ganTracks in the Snow (BOI13_tracks)C++17
100 / 100
677 ms138852 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 4005;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int H, W;
char grid[MX][MX];
int dist[MX][MX];

int main() {
      cin.tie(0); ios_base::sync_with_stdio(0);
  
      for(int i = 0; i < MX; i++)
            for(int j = 0; j < MX; j++)
                  dist[i][j] = -1;
  
      cin >> H >> W;
      for(int i = 0; i < H; i++) {
            for(int j = 0; j < W; j++) {
                  cin >> grid[i][j];
            }       
      }
  
      deque<pair<int,int>> dq;
  
      dq.push_front({0, 0});
      dist[0][0] = 0;
  
      int ans = 0;
      while(!dq.empty()) {
            auto [x, y] = dq.front(); dq.pop_front();
            ans = max(ans, dist[x][y]);
            for(int k = 0; k < 4; k++) {
                  int nx = x + dx[k], ny = y + dy[k];
                  if(nx >= 0 && nx < H && ny >= 0 && ny < W && grid[nx][ny] != '.' && dist[nx][ny] == -1) {
                        if(grid[x][y] == grid[nx][ny]) {
                              dist[nx][ny] = dist[x][y];
                              dq.push_front({nx, ny});
                        } else {
                              dist[nx][ny] = dist[x][y] + 1;
                              dq.push_back({nx, ny});
                        }
                  }
            }
      }
  
      cout << ans + 1 << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...