This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author : pham van sam
* created : 14 June 2023 (Wednesday)
**/
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
const int MAXN = 1e3 + 5;
const int dir[] = {1, -1, 0, 0, 0, 0, 1, -1};
int N, M, dp[MAXN][MAXN];
char a[MAXN][MAXN];
void process(void) {
cin >> N >> M;
FOR(i, 1, N) FOR(j, 1, M) cin >> a[i][j];
deque <pair <int, int>> q;
memset(dp, 0x3f, sizeof dp);
q.emplace_front(1, 1); dp[1][1] = 1;
while(!q.empty()) {
auto [u, v] = q.front(); q.pop_front();
REP(i, 4) {
int x = u + dir[i];
int y = v + dir[i + 4];
if(x < 1 || x > N || y < 1 || y > M) continue;
if(a[x][y] == '*') continue;
int cost = (a[x][y] != a[u][v] ? 1 : 0);
if(minimize(dp[x][y], dp[u][v] + cost)) {
if(!cost) q.emplace_front(x, y);
else q.emplace_back(x, y);
}
}
}
int ans = 0;
FOR(i, 1, N) FOR(j, 1, M) if(a[i][j] != '*') {
maximize(ans, dp[i][j]);
}
cout << ans;
}
signed main() {
#define TASK "TASK"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
auto start_time = steady_clock::now();
int test = 1;
// cin >> test;
for (int i = 1; i <= test; ++i) {
process();
// cout << '\n';
}
auto end_time = steady_clock::now();
cerr << "\nExecution time : " << duration_cast<milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
Compilation message (stderr)
zoo.cpp: In function 'int main()':
zoo.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
zoo.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |