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 mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
int n, m, X, Y, ans;
char g[4005][4005];
bool u[4005][4005], t;
queue<pii> q[2];
void bfs(){
//cout << "*";
q[0].push(mp(1, 1));
for (ans=0; !q[t].empty(); ans++){
//cout << "*";
while(!q[t].empty()){
//cout << "*";
X=q[t].front().x; Y=q[t].front().y; q[t].pop();
if (u[X][Y]) continue;
u[X][Y]=1;
//cout << X << ' ' << Y << ' ' << g[X][Y]<< endl;
if (!(X+1>n || g[X+1][Y]=='.' || u[X+1][Y]))
q[(g[X+1][Y]!=g[X][Y]) != t].push(mp(X+1, Y));
if (!(X-1<1 || g[X-1][Y]=='.' || u[X-1][Y]))
q[(g[X-1][Y]!=g[X][Y]) != t].push(mp(X-1, Y));
if (!(Y+1>m || g[X][Y+1]=='.' || u[X][Y+1]))
q[(g[X][Y+1]!=g[X][Y]) != t].push(mp(X, Y+1));
if (!(Y-1<1 || g[X][Y-1]=='.' || u[X][Y-1]))
q[(g[X][Y-1]!=g[X][Y]) != t].push(mp(X, Y-1));
}
t=!t;
}
}
int main(){
scanf("%i%i", &n, &m);
fox1(l, n){
fox1(l2, m){
scanf(" %c", &g[l][l2]);
}
}
bfs();
cout << ans << endl;
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:54:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i%i", &n, &m);
^
tracks.cpp:57:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c", &g[l][l2]);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |