#include <bits/stdc++.h> // Include every standard library
// Common file
#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<pll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<ld> vd;
typedef vector<vd> vvd;
typedef pair<ll,pll> ppll;
double EPS = 1e-9;
int INF = 2000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 };
#define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (ll(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (ll(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define RESET(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define tiii tuple<int,int,int>
#define mt make_tuple
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define MAX 10000100
// #define mo 998244353
#define mo 1000000007
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
// Begin DEBUG //
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << '{' << p.first << ", " << p.second << '}';
}
template <class T, class = decay_t<decltype(*begin(declval<T>()))>,
class = enable_if_t<!is_same<T, string>::value>>
ostream &operator<<(ostream &os, const T &c) {
os << '[';
for (auto it = c.begin(); it != c.end(); ++it)
os << &", "[2 * (it == c.begin())] << *it;
return os << ']';
}
//support up to 5 args
#define _NTH_ARG(_1, _2, _3, _4, _5, _6, N, ...) N
#define _FE_0(_CALL, ...)
#define _FE_1(_CALL, x) _CALL(x)
#define _FE_2(_CALL, x, ...) _CALL(x) _FE_1(_CALL, __VA_ARGS__)
#define _FE_3(_CALL, x, ...) _CALL(x) _FE_2(_CALL, __VA_ARGS__)
#define _FE_4(_CALL, x, ...) _CALL(x) _FE_3(_CALL, __VA_ARGS__)
#define _FE_5(_CALL, x, ...) _CALL(x) _FE_4(_CALL, __VA_ARGS__)
#define FOR_EACH_MACRO(MACRO, ...) \
_NTH_ARG(dummy, ##__VA_ARGS__, _FE_5, _FE_4, _FE_3, _FE_2, _FE_1, _FE_0) \
(MACRO, ##__VA_ARGS__)
//Change output format here
#define out(x) #x " = " << x << "; "
#define debug(...) \
cout << "Line " << __LINE__ << ": " FOR_EACH_MACRO(out, __VA_ARGS__) << "\n"
// End DEBUG //
ll add(ll a, ll b, ll c = mo) {ll res=(a+b)%c; return res;}
ll mod_neg(ll a, ll b, ll c = mo) {ll res=(a-b+c)%c; return res;}
ll mul(ll a, ll b, ll c = mo) {ll res=(a*b)%c; res=(res+c)%c; return res;}
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const
{
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
if (hash1 != hash2) {
return hash1 ^ hash2;
}
// If hash1 == hash2, their XOR is zero.
return hash1;
}
};
int MOD = 1000000007;
struct mi {
int v;
explicit operator int() const { return v; }
mi() { v = 0; }
mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi &operator+=(mi &a, mi b) {
if ((a.v += b.v) >= MOD) a.v -= MOD;
return a;
}
mi &operator-=(mi &a, mi b) {
if ((a.v -= b.v) < 0) a.v += MOD;
return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long)a.v * b.v); }
mi &operator*=(mi &a, mi b) { return a = a * b; }
mi pow(mi a, long long p) {
assert(p >= 0);
return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator/(mi a, mi b) { return a * inv(b); }
typedef vector<mi> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;
// Clear the stringstream
// ss.str("");
// ss.clear(); // Clear the stringstream state
// ss.str(str2);
///////////////////////////////////////////
/* Main starts here -> Mukund Krishnatrey*/
///////////////////////////////////////////
bool isValid(ll n, ll m, ll r, ll c){
if(r>=0 && r<n && c>=0 && c<m) return true;
else return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("threesum.in", "r")) {
freopen("threesum.in", "r", stdin);
freopen("threesum.out", "w", stdout);
}
cout<<fixed<<setprecision(10);
// ll t_t; cin>>t_t;
// while(t_t--){
ll h,w; cin>>h>>w;
vvc ar(h+1, vc(w+1));
rep(i,h) rep(j,w) cin>>ar[i][j];
deque<pll> dq;
dq.pb(mp(0,0));
vvl dist(h+1, vl(w+1, INF));
dist[0][0]=0;
ll r,c,nr,nc;
vvb vis(h+1, vb(w+1,false));
ll dirn[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
while(!dq.empty()){
auto p=dq.front(); dq.pop_front();
r=p.ff; c=p.ss;
if(vis[r][c]) continue;
vis[r][c]=true;
rep(i,4){
nr=r+dirn[i][0]; nc=c+dirn[i][1];
if(isValid(h,w,nr,nc) && ar[nr][nc]!='.'){
if(ar[nr][nc]==ar[r][c]){
dist[nr][nc]=dist[r][c];
dq.pf(mp(nr,nc));
}
else{
dist[nr][nc]=dist[r][c]+1;
dq.pb(mp(nr,nc));
}
}
}
}
ll ans=0;
rep(i,h) rep(j,w) if(ar[i][j]!='.') ans=max(ans,dist[i][j]);
cout<<ans<<endl;
// }
return 0;
}
// ** Don't use bitwise operators in conditional true and false checks.
// ** Always assert for sanity checks if value should be in a certain range.
// ** In getting n^p, power p shouldn't be reduced by mod. So when obtaining the value of p, don't use any mod operations.
// ** Instead of -1, INT_MIN, etc. use INFF and -INFF if long long values are allowed.
// Consider possible values if >INT_MAX ( or < INT_MIN) don't use it.
// Take care that when erasing values from vector/array/related containers, that the iterator position is gone after that. And even in map/set/multiset, that iterator has to be replaced, can't do it-- or it++ after erase.
// When comparing values, for eg:-, two vectors of equal size, if every value is equal, return false or return v[0]<v[1]. If you return true, then you get segmentation fault.
// Multiset value erase will remove all instances of that value, better to use a map
// Try to divide problem into subcases, and deal with them independently.
// If lexicographical, then definitely don't do any swaps in your code, as it will change the order
// Please read constraints carefully, you might end up overcomplicating the question if not.
// Always verify your solution with atleast the given examples, and some extra edge cases you can think of, before you start coding it
// Have a pseduo structure of code written down, (or in your mand), before writing the same
// Write code in batches, keep testing every independent function, before writing next part
// Check for long long, too many mods
// Always take a look at the examples if stuck, or can't come with a solution for a while, normally examples contain a hint.
// <------ For finding solution to adhoc problem ------>
// ** Draw lots of small cases to gain a better understanding of the problem. If you're having trouble debugging, draw more cases. If you don't know how to start with a problem, draw more cases. Whenever you don't know how to further approach a problem, you're probably massing an important observation, so draw more cases and make observations about properties of the problem.
// ** Whenever you find an observation that seems useful, write it down! Writing down ideas lets you easily come back to them later, and makes sure you don't forget about ideas that could potentially be the solution.
// ** Don't get stuck on any specific idea, unless you see an entire solution.
// ** Try to approach the problem from a lot of different perspectives. Try to mess around with formulas or draw a visual depiction of the problem. One of the most helpful things you can do when solving ad hoc problems is to keep trying ideas until you make progress. This is something you get better at as you do more problems.
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
tracks.cpp:188:9: note: in expansion of macro 'rep'
188 | rep(i,h) rep(j,w) cin>>ar[i][j];
| ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
tracks.cpp:188:18: note: in expansion of macro 'rep'
188 | rep(i,h) rep(j,w) cin>>ar[i][j];
| ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
tracks.cpp:201:13: note: in expansion of macro 'rep'
201 | rep(i,4){
| ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
tracks.cpp:216:9: note: in expansion of macro 'rep'
216 | rep(i,h) rep(j,w) if(ar[i][j]!='.') ans=max(ans,dist[i][j]);
| ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
tracks.cpp:216:18: note: in expansion of macro 'rep'
216 | rep(i,h) rep(j,w) if(ar[i][j]!='.') ans=max(ans,dist[i][j]);
| ^~~
tracks.cpp:180:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
180 | freopen("threesum.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
181 | freopen("threesum.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
3164 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
2908 KB |
Output is correct |
5 |
Correct |
3 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
3 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
6 ms |
1412 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
13 ms |
3160 KB |
Output is correct |
16 |
Correct |
18 ms |
3164 KB |
Output is correct |
17 |
Correct |
10 ms |
2904 KB |
Output is correct |
18 |
Correct |
8 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1368 KB |
Output is correct |
2 |
Correct |
62 ms |
16456 KB |
Output is correct |
3 |
Correct |
373 ms |
159348 KB |
Output is correct |
4 |
Correct |
99 ms |
37980 KB |
Output is correct |
5 |
Correct |
231 ms |
89836 KB |
Output is correct |
6 |
Correct |
1074 ms |
227704 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
2 ms |
1484 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
56 ms |
16228 KB |
Output is correct |
14 |
Correct |
31 ms |
9612 KB |
Output is correct |
15 |
Correct |
21 ms |
10332 KB |
Output is correct |
16 |
Correct |
31 ms |
6960 KB |
Output is correct |
17 |
Correct |
161 ms |
41040 KB |
Output is correct |
18 |
Correct |
107 ms |
40552 KB |
Output is correct |
19 |
Correct |
96 ms |
37900 KB |
Output is correct |
20 |
Correct |
80 ms |
34600 KB |
Output is correct |
21 |
Correct |
207 ms |
93008 KB |
Output is correct |
22 |
Correct |
224 ms |
90020 KB |
Output is correct |
23 |
Correct |
278 ms |
77636 KB |
Output is correct |
24 |
Correct |
188 ms |
90956 KB |
Output is correct |
25 |
Correct |
569 ms |
159312 KB |
Output is correct |
26 |
Incorrect |
966 ms |
624536 KB |
Output isn't correct |
27 |
Correct |
971 ms |
354988 KB |
Output is correct |
28 |
Correct |
1127 ms |
227652 KB |
Output is correct |
29 |
Correct |
1069 ms |
222808 KB |
Output is correct |
30 |
Correct |
994 ms |
262464 KB |
Output is correct |
31 |
Correct |
705 ms |
105764 KB |
Output is correct |
32 |
Correct |
946 ms |
333692 KB |
Output is correct |