// time-limit: 3000
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
using namespace std;
#define ll long long
using vll = vector<ll>;
using vvll = vector<vector<ll> >;
using mpll = map<pair<ll, ll>, ll>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pi = pair<int, int>;
void setIO(string name = "") {
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
void solve() {
return;
}
int weight[4001][4001];
char m[4001][4001];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// setIO("test");
// for optimal solution, each color crosses meadow alternatingly
// if same color cross >1 in a row, can just go back from end point for all the subsequent paths
// construct tree that connects each continguous area of one color to all adjacent blobs of area of the other color
// depth of that tree is min paths (the answer)
//
// alternatively, directly find the node whose shortest path from 0,0 is furthest (in terms of sum of
// weight of edges in the path to that node), weight of adjacent cell of same color = 0, diff color = 1
// the length of the furthest shortest-path is the ans
int h, w;
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> m[i][j];
}
}
memset(weight, -1, sizeof(weight));
weight[0][0] = 0;
int di[4] = {0,0,-1,1};
int dj[4] = {-1,1,0,0};
int res = 0;
deque<pair<int,int>> dq;
dq.push_back({0,0});
while (!dq.empty()) {
auto p = dq.front();
dq.pop_front();
int ww = weight[p.first][p.second];
res = max(res, ww);
for (int i = 0; i < 4; i++) {
int ii = p.first + di[i];
int jj = p.second + dj[i];
if (ii < 0 || jj < 0 || ii >= h || jj >= w) continue;
if (m[ii][jj] == '.') continue;
if (weight[ii][jj] != -1) continue;
if (m[ii][jj] == m[p.first][p.second] ) {
weight[ii][jj] = ww;
dq.push_front({ii, jj});
} else {
weight[ii][jj] = ww + 1;
dq.push_back({ii, jj});
}
}
}
cout << res + 1 << endl;
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |