#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;
#define int long long
#define ld long double
#define inf 1e18
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define vi vector<int>
#define vvi vector< vector<int> >
#define vii vector< pair<int,int> >
#define f(i,a,b) for(int i=a;i<b;i++)
#define Sort(a) sort(a.begin(),a.end())
#define all(x) (x).begin(), (x).end()
#define mii map<int,int>
#define mvi map<int,vi>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define sz(x) (int)(x).size()
const int mod=1e9+7;
typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // *find_by_order(idx), order_of_key(key)
void input(vector<int>&v){
for(int i=0;i<sz(v);i++) cin>>v[i];
}
int cel(int a,int b){
int res=a/b;
if(b*res!=a)res+=(a>0)&(b>0);
return res;
}
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(long double t) {cerr << t;}
void _print(double t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
int binmult(int a , int b , int m){
// for calculating a*b without making an overflow
int ans=0;
while(b){
if(b&1){
ans=(ans+a)%m;
}
a=(a+a)%m;
b>>=1;
}
return ans;
}
int Binexp(int a , int b , int M){
// call this when M is large
a%=M ;// for large a
int ans=1;
while(b){
if(b&1){
ans=binmult(ans,a,M);
}
a=binmult(a,a,M);
b>>=1;
}
return ans;
}
int binexp(int a, int b, int m)
{
int ans = 1;
while (b)
{
if (b&1)
{
ans = ((ans*1LL*a)%m);
}
a = ((a*1LL*a)%m);
b>>=1;
}
return ans;
}
void primefac(int n , map<int,int> &m){
while(n%2==0){
n/=2;
m[2]++;
}
for(int j=3;j<=sqrt(n);j+=2){
while(n%j==0){
n/=j;
m[j]++;
}
}
if(n!=1) m[n]++;
}
void solve(){
int n,m; cin>>n>>m;
vector<string>grid;
for(int i=0; i<n; i++){
string s; cin>>s;
grid.push_back(s);
}
// todo: find the layers in the grid, like how deep can we go into the grid
vector<vector<int>>depth(n+1, vector<int>(m+1,0));
vector<vector<int>>vis(n+1, vector<int>(m+1,0));
int drow[4] = {-1,0,1,0};
int dcol[4] = {0,-1,0,1};
deque<pair<int,int>>q;
q.push_back({0,0}); // top-left corner
vis[0][0]=1;
while(!q.empty()){
int row = q.front().first;
int col = q.front().second;
q.pop_front();
for(int i=0;i<4; i++){
int nrow = row + drow[i];
int ncol = col + dcol[i];
if( nrow >=0 && ncol >=0 && nrow<n && ncol<m && !vis[nrow][ncol] ){
vis[nrow][ncol] = 1;
if ( grid[nrow][ncol] == grid[row][col] ){
depth[nrow][ncol] = depth[row][col];
q.push_front({nrow,ncol});
}
else{
depth[nrow][ncol] = depth[row][col] + 1;
q.push_back({nrow,ncol});
}
debug(grid);
}
}
}
int maxi = 1LL;
for(int i=0; i<n; i++){
for(int j=0; j<m;j++){
maxi = max(maxi, depth[i][j]);
}
}
cout<<maxi<<"\n";
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int32_t main()':
tracks.cpp:168:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | freopen("Error.txt", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:169:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:170:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
170 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |