Submission #1094830

#TimeUsernameProblemLanguageResultExecution timeMemory
1094830wrttTracks in the Snow (BOI13_tracks)C++14
9.90 / 100
2105 ms1048576 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 << "]";
}
*/


void solve() {
  int h,w;
  cin >> h >> w;
  int a[h][w];
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      if(c=='F') {
        a[i][j] = 1;
      }
      else if(c=='R') {
        a[i][j] = 0;
      }
      else {
        a[i][j] = 2;
      }
    }
  }
  vector<pair<int,int>> g[h*w+1];
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      if(a[i][j] != 2) {
        if(i+1 < h && a[i+1][j] != 2) {
          g[i*w+j].pb((i+1)*w+j, a[i][j] != a[i+1][j]);
          g[(i+1)*w+j+1].pb(i*w+j, a[i][j] != a[i+1][j]);
        }
        if(j+1 < w && a[i][j+1] != 2) {
          g[i*w+j].pb(i*w+j+1, a[i][j] != a[i][j+1]);
          g[i*w+j+1].pb(i*w+j, a[i][j] != a[i][j+1]);
        }
        if(i-1 >= 0 && a[i-1][j] != 2) {
          g[i*w+j].pb((i-1)*w+j, a[i][j] != a[i-1][j]);
          g[(i-1)*w+j+1].pb(i*w+j, a[i][j] != a[i-1][j]);
        }
        if(j-1 >= 0 && a[i][j-1] != 2) {
          g[i*w+j].pb(i*w+j-1, a[i][j] != a[i][j-1]);
          g[i*w+j-1].pb(i*w+j, a[i][j] != a[i][j-1]);
        }
      }
    }
  }
  // 0-1 BFS
  int dist[h*w+1];
  for(int i = 0; i < h*w+1; ++i) {
    dist[i] = 1e18;
  }
  dist[0] = 0;
  deque<int> q;
  q.push_back(0);
  while(!q.empty()) {
    int c = q.front();
    q.pop_front();
    for(auto u : g[c]) {
      int v = u.first;
      int w = u.second;
      if(dist[v] > dist[c]+w) {
        dist[v] = dist[c]+w;
        if(w) {
          q.push_front(v);
        }
        else {
          q.push_back(v);
        }
      }
    }
  }
  int ans = 0;
  for(int i = 0; i < h*w+1; ++i) {
    if(dist[i] != 1e18) {
      ans=mx(ans, dist[i]);
    }
  }
  cout<<ans+1<<"\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...