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 pi pair<int,int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
const int N = 4e3+10;
char a[N][N];
int visited[N][N];
queue<pi> q;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int r,c;cin >> r >> c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin >> a[i][j];
}
}
visited[1][1] = 1;
q.push({1,1});
set<char> ans;
while(sz(q)){
int ni = q.front().first,nj = q.front().second;
ans.insert(a[ni][nj]);
q.pop();
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){
if(abs(i) == abs(j)) continue;
int ii = ni+i,jj = nj+j;
if(ii < 1 || ii > r || jj < 1 || jj > c) continue;
if(visited[ii][jj]) continue;
visited[ii][jj] = 1;
q.push({ii,jj});
}
}
cout << sz(ans);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |