#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using std::mt19937_64;
using std::random_device;
using std::uniform_int_distribution;
#include <numeric>
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void cpc()
{
#ifndef ONLINE_JUDGE
freopen("shuffle.in", "r", stdin);
freopen("shuffle.out", "w", stdout);
#endif
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int exp(int x, int n, int m) {
if(x == 0 and n == 0) return 1;
x %= m;
int res = 1;
while (n > 0) {
if (n % 2 == 1) { res = res * x % m; }
x = x * x % m;
n /= 2;
}
return res;
}
int gcd(int a,int b){
if(a == 0) return b;
return gcd(b%a,a);
}
bool check(int x,int y,int n,int m){
if(x < 0 or y < 0 or x >= n or y >= m) return false;
return true;
}
bool valid(int &x,int &m){
int prev = x&1;
for(int i = 1 ; i < m; i++){
if((x&(1<<i)) == 0 and prev == 0) return false;
prev = (x&(1<<i));
}
return true;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cpc();
int t=1;
// cin >> t;
while(t--){
int n,m;
cin >> n >> m;
vector<vector<char>> v(n,vector<char>(m));
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < m; j++){
cin >> v[i][j];
}
}
deque<pair<int,int>> dq;
dq.push_back({0,0});
vector<vector<int>> visited(n,vector<int>(m,0));
visited[0][0] = 1;
int ans = 1;
while(!dq.empty()){
int i = dq.front().ff;
int j = dq.front().ss;
ans = max(ans,visited[i][j]);
dq.pop_front();
for(int ind = 0 ; ind < 4; ind++){
int x = i+dx[ind];
int y = j+dy[ind];
if(check(x,y,n,m) and visited[x][y]==0 and v[x][y] != '.'){
if(v[i][j] == v[x][y]){
dq.push_front({x,y});
visited[x][y] = visited[i][j];
}
else{
dq.push_back({x,y});
visited[x][y] = visited[i][j]+1;
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'void cpc()':
tracks.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen("shuffle.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen("shuffle.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |