Submission #1094850

#TimeUsernameProblemLanguageResultExecution timeMemory
1094850wrttTracks in the Snow (BOI13_tracks)C++14
100 / 100
693 ms222212 KiB
#include "bits/stdc++.h"

using namespace std;

#define int long long
#define pb emplace_back
#define mx(a, b) ((a) > (b) ? (a) : (b))
#define mn(a, b) ((a) < (b) ? (a) : (b))
#define graph(n) vector<int> g[(n)+1];
constexpr int mod = 1000000007;

void frx(std::string name) {
  freopen((name + ".in").c_str(), "r", stdin);
  freopen((name + ".out").c_str(), "w", stdout);
}

/*
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &os, const T (&arr)[N]) {
  os << "[";
  for(size_t i = 0; i < N; ++i) {
    if(i>0) {
      os << ", ";
    }
    os<<arr[i];
  }
  return os << "]";
}
*/
int h,w;
char a[4001][4001];

bool is(int i, int j) {
  return i>=0 && i<h && j>=0 && j<w && a[i][j]!='.';
}


void solve() {
  cin >> h >> w;
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      cin>>a[i][j];
    }
  }
  int dist[h][w];
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      dist[i][j] = 1e18;
    }
  }
  dist[0][0] = 1;
  deque<pair<int,int>> q;
  q.push_front({0,0});
  int ans = 0;
  while(!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    ans=mx(ans, dist[x][y]);
    if(is(x-1,y)) {
      if(dist[x-1][y] >dist[x][y]+(a[x][y] != a[x-1][y])) {
        dist[x-1][y] = dist[x][y]+(a[x][y] != a[x-1][y]);
        if(a[x][y] != a[x-1][y]) {
          q.push_back({x-1,y});
        } else {
          q.push_front({x-1,y});
        }
      } 
    }
    if(is(x+1,y)) {
      if(dist[x+1][y] > dist[x][y]+(a[x][y] != a[x+1][y])) {
        dist[x+1][y] = dist[x][y]+(a[x][y] != a[x+1][y]);
        if(a[x][y] != a[x+1][y]) {
          q.push_back({x+1,y});
        } else {
          q.push_front({x+1,y});
        }
      } 
    }
    if(is(x,y-1)) {
      if(dist[x][y-1] > dist[x][y]+(a[x][y] != a[x][y-1])) {
        dist[x][y-1] = dist[x][y]+(a[x][y] != a[x][y-1]);
        if(a[x][y] != a[x][y-1]) {
          q.push_back({x,y-1});
        } else {
          q.push_front({x,y-1});
        }
      } 
    }
    if(is(x,y+1)) {
      if(dist[x][y+1] > dist[x][y]+(a[x][y] != a[x][y+1])) {
        dist[x][y+1] = dist[x][y]+(a[x][y] != a[x][y+1]);
        if(a[x][y] != a[x][y+1]) {
          q.push_back({x,y+1});
        } else {
          q.push_front({x,y+1});
        }
      } 
    }
  }
  std::cout<<ans<<"\n";
}


signed main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    //frx("perimeter");
    #ifdef LOCAL  
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    auto start = std::chrono::high_resolution_clock::now();
    #endif
    int t = 1;
    //std::cin >> t;
    while (t--) {
        solve();
    }
    #ifdef LOCAL
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << elapsed.count() << " seconds\n";
    #endif
    return 0;  
}

Compilation message (stderr)

tracks.cpp: In function 'void frx(std::string)':
tracks.cpp:13:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |   freopen((name + ".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:14:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |   freopen((name + ".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...