简单bfs,暴露了我长久不打代码啥都忘光光的现实。
#include <bits/stdc++.h>
using namespace std;
const int N= 1e3 + 5 ;
const int dx[ 8 ] = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } , dy[ 8 ] = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ;
int n, sg, sf, ans1, ans2;
int a[ N] [ N] ;
bool vis[ N] [ N] ;
struct node{ int x, y; } ;
queue< node> q;
inline void bfs ( int x, int y)
{
q. push ( ( node) { x, y} ) ;
vis[ x] [ y] = true ;
while ( q. size ( ) )
{
node u= q. front ( ) ; q. pop ( ) ;
for ( register int i= 0 ; i< 8 ; ++ i)
{
int xx= u. x+ dx[ i] , yy= u. y+ dy[ i] ;
if ( xx< 1 || xx> n || yy< 1 || yy> n) continue ;
if ( a[ xx] [ yy] > a[ u. x] [ u. y] ) sf= 0 ;
if ( a[ xx] [ yy] < a[ u. x] [ u. y] ) sg= 0 ;
if ( a[ xx] [ yy] == a[ u. x] [ u. y] && ! vis[ xx] [ yy] ) q. push ( ( node) { xx, yy} ) , vis[ xx] [ yy] = true ;
}
}
}
inline int read ( )
{
int ret= 0 , ff= 1 ; char ch= getchar ( ) ;
while ( ! isdigit ( ch) ) { if ( ch== '-' ) ff= - ff; ch= getchar ( ) ; }
while ( isdigit ( ch) ) { ret= ( ret<< 3 ) + ( ret<< 1 ) + ch- '0' ; ch= getchar ( ) ; }
return ret* ff;
}
int main ( ) {
n= read ( ) ;
for ( register int i= 1 ; i<= n; ++ i)
for ( register int j= 1 ; j<= n; ++ j) a[ i] [ j] = read ( ) ;
for ( register int i= 1 ; i<= n; ++ i)
for ( register int j= 1 ; j<= n; ++ j)
if ( ! vis[ i] [ j] )
{
sf= sg= 1 ;
bfs ( i, j) ;
ans1+ = sf; ans2+ = sg;
}
printf ( "%d %d\n" , ans1, ans2) ;
return 0 ;
}