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>
//#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 4005;
const int mod7 = 1e9+7;
const long long inf = 1e9;
struct str
{
int a,b,c;
};
vector<vector<int>> dist(MAXN, vector<int>(MAXN,inf));
deque<str> pq;
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
int n,m;
cin >> n >> m;
vector<string>mat(n);
for(int i=0; i<n; i++)cin >> mat[i];
dist[0][0] = 0;
pq.pb({0,0,0});
while(!pq.empty())
{
auto it = pq.front();
pq.pop_front();
int i = it.b;
int j = it.c;
int d = it.a;
if(i>0 && mat[i-1][j]!='.')
{
int cost = (mat[i][j] != mat[i-1][j]);
if(dist[i-1][j] > dist[i][j] + cost)
{
dist[i-1][j] = dist[i][j] + cost;
if(cost)pq.push_back({dist[i-1][j], i-1, j});
else pq.push_front({dist[i-1][j], i-1, j});
}
}
if(i<n-1&& mat[i+1][j]!='.')
{
int cost = (mat[i][j] != mat[i+1][j]);
if(dist[i+1][j] > dist[i][j] + cost)
{
dist[i+1][j] = dist[i][j] + cost;
if(cost)pq.push_back({dist[i+1][j], i+1, j});
else pq.push_front({dist[i+1][j], i+1, j});
}
}
if(j>0&& mat[i][j-1]!='.')
{
int cost = (mat[i][j] != mat[i][j-1]);
if(dist[i][j-1] > dist[i][j] + cost)
{
dist[i][j-1] = dist[i][j] + cost;
if(cost)pq.push_back({dist[i][j-1], i, j-1});
else pq.push_front({dist[i][j-1], i, j-1});
}
}
if(j<m-1 && mat[i][j+1]!='.')
{
int cost = (mat[i][j] != mat[i][j+1]);
if(dist[i][j+1] > dist[i][j] + cost)
{
dist[i][j+1] = dist[i][j] + cost;
if(cost)pq.push_back({dist[i][j+1], i, j+1});
else pq.push_front({dist[i][j+1], i, j+1});
}
}
}
int mx = 0;
for(int i = 0;i<n; i++)
{
for(int j=0; j<m; j++)if(dist[i][j]!=inf)mx = max(mx, dist[i][j]);
}
cout << mx+1 << endl;
}
}
/*
3 4
FFRR
RFRF
RFRF
*/
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:44:17: warning: unused variable 'd' [-Wunused-variable]
44 | int d = it.a;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |