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 ll long long
#define sz(x) (x).size()
#define all(x) x.begin() , x.end()
#define F first
#define S second
void Sonic(string name = "") {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if(sz(name)){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
int xd[] = {0, 1, -1, 0, 1, 1, -1, -1};
int yd[] = {1, 0, 0, -1, 1, -1, 1, -1};
const int mod = 1e9 + 7, MAXN = 1e6 + 5;
void run_test_case(){
int n,m;cin>>n>>m;
char grid[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)cin>>grid[i][j];
}
vector<vector<int>>dist(n,vector<int>(m,INT_MAX));
deque<pair<int,int>>dq;
dist[0][0] = 1;
dq.push_back({0,0});
while(!dq.empty()){
int i = dq.front().first, j = dq.front().second;
dq.pop_front();
for(int k = 0; k < 4; k++){
int ni = i+xd[k], nj = j+yd[k];
if(ni<0||nj<0||ni>=n||nj>=m||grid[ni][nj]=='.')continue;
bool f = (grid[i][j] == grid[ni][nj]);
if(dist[i][j] + !f < dist[ni][nj]){
if(!f)dq.push_back({ni,nj});
else dq.push_front({ni,nj});
dist[ni][nj] = dist[i][j]+!f;
}
}
}
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(dist[i][j]==INT_MAX)continue;
ans = max(ans,dist[i][j]);
}
}
cout<<ans;
}
int main(){
Sonic();
int T = 1;
//cin>>T;
while(T--){
run_test_case();
}
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'void Sonic(std::string)':
tracks.cpp:11:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen((name+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((name+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |