# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int N = 1010 ;
char g[ N] [ N] ;
int n, m, res;
void bfs ( int sx, int sy)
{
queue< pii> q;
q. push ( { sx, sy} ) ;
g[ sx] [ sy] = '.' ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int i = - 1 ; i <= 1 ; i ++ )
{
for ( int j = - 1 ; j <= 1 ; j++ )
{
if ( i == 0 && j == 0 ) continue ;
int nx = t. x + i, ny = t. y + j;
if ( nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if ( g[ nx] [ ny] == 'W' )
{
g[ nx] [ ny] = '.' ;
q. push ( { nx, ny} ) ;
}
}
}
}
}
}
int main ( )
{
cin >> n >> m;
for ( int i = 0 ; i < n; i++ ) cin >> g[ i] ;
for ( int i = 0 ; i < n; i++ )
{
for ( int j = 0 ; j < m; j++ )
{
if ( g[ i] [ j] == 'W' )
{
res ++ ;
bfs ( i, j) ;
}
}
}
cout << res << endl;
return 0 ;
}
# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int dx[ 4 ] = { 0 , - 1 , 0 , 1 } ;
const int dy[ 4 ] = { - 1 , 0 , 1 , 0 } ;
const int N = 55 ;
int g[ N] [ N] ;
bool st[ N] [ N] ;
int n, m, area;
void bfs ( int sx, int sy)
{
area = 1 ;
queue< pii> q;
q. push ( { sx, sy} ) ;
st[ sx] [ sy] = true ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int k = 0 ; k < 4 ; k++ )
{
int nx = t. x + dx[ k] , ny = t. y + dy[ k] ;
if ( nx >= 1 && nx <= n && ny >= 1 && ny <= m)
{
if ( ! st[ nx] [ ny] && ! ( g[ t. x] [ t. y] >> k & 1 ) )
{
st[ nx] [ ny] = true ;
area++ ;
q. push ( { nx, ny} ) ;
}
}
}
}
}
int main ( )
{
cin >> n >> m;
for ( int i = 1 ; i <= n; i++ )
{
for ( int j = 1 ; j <= m; j++ )
cin >> g[ i] [ j] ;
}
int cnt = 0 , max_area = 0 ;
for ( int i = 1 ; i <= n; i++ )
{
for ( int j = 1 ; j <= m; j++ )
{
if ( ! st[ i] [ j] )
{
bfs ( i, j) ;
cnt ++ ;
max_area = max ( area, max_area) ;
}
}
}
cout << cnt << endl;
cout << max_area << endl;
return 0 ;
}
# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int N = 1010 ;
int g[ N] [ N] ;
bool st[ N] [ N] ;
int n;
void bfs ( int sx, int sy, bool & has_lower, bool & has_higher)
{
queue< pii> q;
q. push ( { sx, sy} ) ;
st[ sx] [ sy] = true ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int i = - 1 ; i <= 1 ; i++ )
{
for ( int j = - 1 ; j <= 1 ; j++ )
{
if ( i == 0 && j == 0 ) continue ;
int nx = t. x + i, ny = t. y + j;
if ( nx >= 1 && nx <= n && ny >= 1 && ny <= n)
{
if ( g[ t. x] [ t. y] == g[ nx] [ ny] && ! st[ nx] [ ny] )
{
st[ nx] [ ny] = true ;
q. push ( { nx, ny} ) ;
}
if ( g[ t. x] [ t. y] < g[ nx] [ ny] ) has_higher = true ;
if ( g[ t. x] [ t. y] > g[ nx] [ ny] ) has_lower = true ;
}
}
}
}
}
int main ( )
{
cin >> n;
for ( int i = 1 ; i <= n; i++ )
{
for ( int j = 1 ; j <= n; j++ )
cin >> g[ i] [ j] ;
}
int peak = 0 , valley = 0 ;
for ( int i = 1 ; i <= n; i++ )
{
for ( int j = 1 ; j <= n; j++ )
{
if ( ! st[ i] [ j] )
{
bool has_lower = false , has_higher = false ;
bfs ( i, j, has_lower, has_higher) ;
if ( ! has_lower) valley ++ ;
if ( ! has_higher) peak ++ ;
}
}
}
cout << peak << ' ' << valley << endl;
return 0 ;
}
# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int dx[ 4 ] = { 1 , 0 , - 1 , 0 } ;
const int dy[ 4 ] = { 0 , - 1 , 0 , 1 } ;
const int N = 10 ;
int g[ N] [ N] ;
pii pre[ N] [ N] ;
bool st[ N] [ N] ;
void bfs ( )
{
queue< pii> q;
q. push ( { 0 , 0 } ) ;
st[ 0 ] [ 0 ] = true ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int k = 0 ; k < 4 ; k++ )
{
int nx = t. x + dx[ k] , ny = t. y + dy[ k] ;
if ( nx >= 0 && nx < 5 && ny >= 0 && ny < 5 )
{
if ( ! st[ nx] [ ny] && g[ nx] [ ny] == 0 )
{
pre[ nx] [ ny] = { t. x, t. y} ;
q. push ( { nx, ny} ) ;
st[ nx] [ ny] = true ;
}
}
}
}
}
int main ( )
{
for ( int i = 0 ; i < 5 ; i++ )
{
for ( int j = 0 ; j < 5 ; j++ )
cin >> g[ i] [ j] ;
}
bfs ( ) ;
vector< pii> path;
pii t = { 4 , 4 } ;
while ( ! ( t. x == 0 && t. y == 0 ) )
{
path. push_back ( t) ;
t = pre[ t. x] [ t. y] ;
}
reverse ( path. begin ( ) , path. end ( ) ) ;
cout << "(0, 0)\n" ;
for ( auto t : path) cout << "(" << t. x << ", " << t. y << ")" << endl;
return 0 ;
}
# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int dx[ 4 ] = { 1 , 0 , - 1 , 0 } ;
const int dy[ 4 ] = { 0 , - 1 , 0 , 1 } ;
const int N = 50 ;
char g[ N] [ N] ;
bool st[ N] [ N] ;
int d[ N] [ N] ;
int n, m;
void bfs ( )
{
queue< pii> q;
q. push ( { 0 , 0 } ) ;
st[ 0 ] [ 0 ] = true ;
d[ 0 ] [ 0 ] = 1 ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int k = 0 ; k < 4 ; k++ )
{
int nx = t. x + dx[ k] , ny = t. y + dy[ k] ;
if ( nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if ( ! st[ nx] [ ny] && g[ nx] [ ny] == '.' )
{
st[ nx] [ ny] = true ;
d[ nx] [ ny] = d[ t. x] [ t. y] + 1 ;
q. push ( { nx, ny} ) ;
}
}
}
}
}
int main ( )
{
cin >> n >> m;
for ( int i = 0 ; i < n; i++ ) cin >> g[ i] ;
bfs ( ) ;
cout << d[ n- 1 ] [ m- 1 ] << endl;
return 0 ;
}
# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int dx[ 8 ] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ;
const int dy[ 8 ] = { - 1 , - 2 , - 2 , - 1 , 1 , 2 , 2 , 1 } ;
const int N = 200 ;
char g[ N] [ N] ;
bool st[ N] [ N] ;
int d[ N] [ N] ;
int n, m;
int sx, sy, gx, gy;
int bfs ( )
{
queue< pii> q;
q. push ( { sx, sy} ) ;
st[ sx] [ sy] = true ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int k = 0 ; k < 8 ; k++ )
{
int nx = t. x + dx[ k] , ny = t. y + dy[ k] ;
if ( nx == gx && ny == gy) return d[ t. x] [ t. y] + 1 ;
if ( nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if ( ! st[ nx] [ ny] && g[ nx] [ ny] == '.' )
{
d[ nx] [ ny] = d[ t. x] [ t. y] + 1 ;
st[ nx] [ ny] = true ;
q. push ( { nx, ny} ) ;
}
}
}
}
return - 1 ;
}
int main ( )
{
cin >> m >> n;
for ( int i = 0 ; i < n; i++ )
{
for ( int j = 0 ; j < m; j++ )
{
cin >> g[ i] [ j] ;
if ( g[ i] [ j] == 'K' ) sx = i, sy = j;
if ( g[ i] [ j] == 'H' ) gx = i, gy = j;
}
}
cout << bfs ( ) << endl;
return 0 ;
}
# include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10 ;
int d[ N] ;
bool st[ N] ;
int s, g;
int bfs ( )
{
queue< int > q;
q. push ( s) ;
d[ s] = 0 ;
st[ s] = true ;
while ( q. size ( ) )
{
int t = q. front ( ) ;
q. pop ( ) ;
if ( t == g) return d[ t] ;
if ( t - 1 >= 0 && ! st[ t- 1 ] )
{
d[ t- 1 ] = d[ t] + 1 ;
st[ t- 1 ] = true ;
q. push ( t - 1 ) ;
}
if ( t + 1 <= g && ! st[ t + 1 ] )
{
d[ t + 1 ] = d[ t] + 1 ;
st[ t + 1 ] = true ;
q. push ( t + 1 ) ;
}
if ( 2 * t < N && ! st[ 2 * t] )
{
d[ 2 * t] = d[ t] + 1 ;
st[ 2 * t] = true ;
q. push ( t * 2 ) ;
}
}
return - 1 ;
}
int main ( )
{
cin >> s >> g;
cout << bfs ( ) << endl;
return 0 ;
}