This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std ;
#define ios ios_base::sync_with_stdio(0); cin.tie(0) ; cout.tie(0)
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define endl '\n'
typedef long long int ll ;
typedef long double lb ;
typedef unsigned long long int ul ;
const ll MOD = 1e9 + 7 ;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1} ;
void solve(){
int h, w ;
cin >> h >> w ;
string s[h] ;
for (int i = 0; i < h; i++) cin >> s[i] ;
deque<pair<int, int>> q ;
int dist[h][w] ;
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dist[i][j] = 0 ;
int ans = 0 ;
q.push_back({0, 0}) ;
dist[0][0] = 1 ;
while (!q.empty()) {
pair<int, int> c = q.front() ;
q.pop_front() ;
ans = max(ans, dist[c.f][c.s]) ;
for (int i = 0; i < 4; i++) {
int x = c.f + dx[i]; int y = c.s + dy[i] ;
if (x < 0 || x >= h || y < 0 || y >= w || s[x][y] == '.') continue ;
if (dist[x][y] == 0) {
if (s[x][y] == s[c.f][c.s]) {
dist[x][y] = dist[c.f][c.s] ;
q.push_front({x, y}) ;
}
else {
dist[x][y] = dist[c.f][c.s] + 1 ;
q.push_back({x, y}) ;
}
}
}
}
cout << ans << endl ;
}
int main(){
ios ;
solve() ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |