#include <iostream>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <iterator>
#include <cstring>
#include <fstream>
#include <map>
#include <bitset>
#include <cassert>
#include <stdlib.h>
#include <climits>
#include <stack>
#include <complex>
using namespace std;
#define fa(a,b)(memset(a,b,sizeof(a)));
#define fill2d(vec,val) for(auto &v:vec)fill(all(v),val)
#define mp(a,b)(make_pair(a,b))
#define pb(a) push_back(a)
#define FORE(i,a,b) for (int i = (a); i <= (b); ++i)
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (a); i >= (b); --i)
#define FR(i,a) FOR(i,0,a)
#define p (pi())
#define flag (cout<<"here"<<endl)
#define fast ios::sync_with_stdio(0);cin.tie(0)
#define sz(x) ((int)(x.size()))
#define f first
#define s second
#define all(x) begin(x), end(x)
#define in(r,R) (r>=0&&r<R)
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pii = pair<int, int>;
using pdd = pair<double, double>;
const int dx[8] = {1,0,-1,0,1,1,-1,-1}; //first 4 are normal
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
const int MOD = 1000000007;
const ll NMOD = 1000000009;
const int AR_MAX = (1 << 7) - 1; //when using memeset
const int AR_MAX_VAL = 2139062143;
//^ Not recommended unless
// there's no other way do to what you need to do
const double PI =3.141592653589793238463;
const double EPS = 10e-9;
void _forAllUnusedConsts() {
//to silence unused variable warnings
cout <<
AR_MAX <<
AR_MAX_VAL<<
dx[0]<<
dy[1]<<
PI<<
EPS<<
endl;
}
template<class Q>
typename Q::value_type pop(Q& q) {
auto result = q.front();
q.pop();
return result;
};
//reading input the "java" way
//(aka with method calls)
int pi() {
int N;
cin >> N;
return N;
}
ll pl() {
ll N;
cin >> N;
return N;
}
char pc() {
char c;
cin >> c;
return c;
}
string ps() {
string s;
cin >> s;
return s;
}
//other
template<class T1, class T2>
ll powf(T1 a, T2 b) {
if(b<0)return 0;
if (b == 0)return 1;
ll val = powf(a, (ll)(b / 2));
if (b & 1) {
return ((val * val) % MOD * a) % MOD;
} else {
return (val * val) % MOD;
}
}
string bin(int n, int digits) {
string s = bitset<32>(n).to_string();
s.erase(0, s.length() - digits);
return s;
}
void IO(string fileName) {
freopen((fileName + ".in").c_str(), "r", stdin);
freopen((fileName + ".out").c_str(), "w", stdout);
}
ll cp(complex<ll> a, complex<ll> b){
return (conj(a)*b).imag();
}
bool eq(double a, double b){
return fabs(a-b)<=EPS;
}
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
1 1
R
*/
int main() {
fast;
int R = p;
int C = p;
char g[R][C];
int dist[R][C]; fa(dist,0);
dist[0][0] = 1;
FR(r,R){
FR(c,C){
char cur = pc();
g[r][c] = cur;
}
}
deque<pii> q;
q.push_back({0,0});
int ans = 1;
while(sz(q)){
pii cur = q.front(); q.pop_front();
FR(i,4){
int r2 = cur.f+dx[i];
int c2 = cur.s+dy[i];
if(in(r2,R)&&in(c2,C)&&g[r2][c2]!='.'&&!dist[r2][c2]){
if(g[r2][c2]==g[cur.f][cur.s]){
dist[r2][c2] = dist[cur.f][cur.s];
q.push_front({r2,c2});
}else{
dist[r2][c2] = dist[cur.f][cur.s]+1;
q.push_back({r2,c2});
}
// cout<<r2<<" "<<c2<<endl;
ans = max(ans,dist[r2][c2]);
}
}
}
cout<<ans<<endl;
return 0;
}
Compilation message
tracks.cpp: In function 'void IO(std::string)':
tracks.cpp:118:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen((fileName + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:119:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen((fileName + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1612 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
9 ms |
1356 KB |
Output is correct |
5 |
Correct |
4 ms |
716 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
5 ms |
716 KB |
Output is correct |
13 |
Correct |
2 ms |
716 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
10 ms |
1612 KB |
Output is correct |
16 |
Correct |
11 ms |
1692 KB |
Output is correct |
17 |
Correct |
9 ms |
1600 KB |
Output is correct |
18 |
Correct |
7 ms |
1404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
50 ms |
8096 KB |
Output is correct |
3 |
Correct |
349 ms |
78644 KB |
Output is correct |
4 |
Correct |
108 ms |
18700 KB |
Output is correct |
5 |
Correct |
236 ms |
44392 KB |
Output is correct |
6 |
Correct |
685 ms |
92596 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
416 KB |
Output is correct |
13 |
Correct |
51 ms |
7884 KB |
Output is correct |
14 |
Correct |
27 ms |
4684 KB |
Output is correct |
15 |
Correct |
25 ms |
5188 KB |
Output is correct |
16 |
Correct |
23 ms |
3440 KB |
Output is correct |
17 |
Correct |
126 ms |
20132 KB |
Output is correct |
18 |
Correct |
95 ms |
19788 KB |
Output is correct |
19 |
Correct |
85 ms |
18636 KB |
Output is correct |
20 |
Correct |
81 ms |
17100 KB |
Output is correct |
21 |
Correct |
205 ms |
45848 KB |
Output is correct |
22 |
Correct |
243 ms |
44316 KB |
Output is correct |
23 |
Correct |
239 ms |
38212 KB |
Output is correct |
24 |
Correct |
200 ms |
44768 KB |
Output is correct |
25 |
Correct |
492 ms |
78564 KB |
Output is correct |
26 |
Correct |
448 ms |
110364 KB |
Output is correct |
27 |
Correct |
618 ms |
113156 KB |
Output is correct |
28 |
Correct |
702 ms |
92584 KB |
Output is correct |
29 |
Correct |
679 ms |
90132 KB |
Output is correct |
30 |
Correct |
649 ms |
96080 KB |
Output is correct |
31 |
Correct |
519 ms |
50972 KB |
Output is correct |
32 |
Correct |
541 ms |
102908 KB |
Output is correct |