#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
#define ii pair< int , int >
#define pb push_back
#define fi first
#define se second
#define all(x) begin(x) , end(x)
#define full(x) x + 1 , x + n + 1
#define MASK(i) 1LL << i
#define bit(x, i) ( (x) & MASK(i) )
#define popcount(i) __builtin_popcountll(i)
#define set_on(x, i) ( (x) | MASK(i) )
#define set_off( x, i ) ( (x) & ~MASK(i) )
using namespace std;
using ll = long long;
using str = string;
constexpr ll P[4] = {'U', 'L', 'R', 'D'};
constexpr ll dx[8] = {-1, -1, 1, 1, 0, 0, -1, 1};
constexpr ll dy[8] = {-1, 1, -1, 1, -1, 1, 0, 0};
constexpr ll L = 1e3 + 5;
constexpr ll LINF = ( 1e18 + 5 );
constexpr ll N = 1e6 + 5;
constexpr ll inf = ( 1e9 + 5 );
constexpr int MOD = 1e9 + 7;
template<class X , class Y>
bool maximize( X& x , const Y& y ){
if ( x < y ){
x = y;
return true;
}
return false;
}
template<class X , class Y>
bool minimize( X& x , const Y& y ){
if ( x > y ){
x = y;
return true;
}
return false;
}
template<class T> using MaxHeap = priority_queue< T , vector<T> , less<T> >;
template<class T> using MinHeap = priority_queue< T , vector<T> , greater<T> >;
queue<ii> meet, Q;
bool vst[L][L];
ll res = 0;
char a[L][L];
ll n, m;
bool isOk( ll x , ll y ){
return x >= 1 && x <= n && y >= 1 && y <= m && !vst[x][y] && a[x][y] != '*';
}
void bfs(){
res++;
auto [ X , Y ] = Q.front();
while ( !Q.empty() ){
auto[ x, y ] = Q.front(); Q.pop();
for ( int i = 4 ; i < 8 ; i++ ){
ll nx = dx[i] + x;
ll ny = dy[i] + y;
if ( isOk( nx, ny ) ){
if ( a[nx][ny] == a[X][Y] ) Q.emplace( nx , ny );
else meet.emplace( nx , ny );
vst[nx][ny] = true;
}
}
}
swap( meet, Q );
}
void Whisper(){
cin >> n >> m;
for ( int i = 1 ; i <= n ; i++ ){
for ( int j = 1 ; j <= m ; j++ ){
cin >> a[i][j];
}
}
vst[1][1] = true;
Q.emplace( 1, 1 );
while ( !Q.empty() )
bfs();
cout << res;
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
#define TASK "__Whisper"
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
ll Test = 1; //cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
//
cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
12 ms |
3376 KB |
Output is correct |
17 |
Correct |
12 ms |
3164 KB |
Output is correct |
18 |
Correct |
12 ms |
3420 KB |
Output is correct |
19 |
Correct |
13 ms |
3164 KB |
Output is correct |
20 |
Correct |
12 ms |
3164 KB |
Output is correct |
21 |
Correct |
36 ms |
3172 KB |
Output is correct |
22 |
Correct |
36 ms |
3160 KB |
Output is correct |
23 |
Correct |
35 ms |
3164 KB |
Output is correct |
24 |
Correct |
37 ms |
3160 KB |
Output is correct |
25 |
Correct |
36 ms |
3164 KB |
Output is correct |
26 |
Correct |
40 ms |
3280 KB |
Output is correct |
27 |
Correct |
37 ms |
3416 KB |
Output is correct |
28 |
Correct |
35 ms |
3160 KB |
Output is correct |
29 |
Correct |
37 ms |
3388 KB |
Output is correct |
30 |
Correct |
38 ms |
3316 KB |
Output is correct |