dfs思路,map被卡了,换成unordered_mapAC,用时1600ms左右
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<unordered_map>
using namespace std;
#define int long long
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define DEBUG freopen("input.txt", "r", stdin);
#define Ccase(x) printf("Case %lld: ", 1LL * x++);
#define pb(x) push_back(x)
const int INF = 1e18;
/////////////////////////////////////////////////////////////
const int N = 1e3 + 17;
int a[N][N], b[N][N];
int n, m;
int color = 0;
int ansup = 0, ansdown = 0;
//int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1};
unordered_map<int, int> mm;
int g(int x, int y){
return x > 0 && x <= n && y > 0 && y <= m;
}
int flag;
void f(int x, int y, int& up, int& down){
//cout << x << ' ' << y << endl;
if(b[x][y]) return;
if(flag) color++;
b[x][y] = color;
flag = 0;
for(int i = 0; i < 8; ++i){
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(g(xx, yy)){
if(a[xx][yy] == a[x][y]) f(xx, yy, up, down);
else if(a[xx][yy] > a[x][y]) down++;
else up++;
}
}
}
void solve(){
cin >> n;
m = n;
map<int, int> m2;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j){
//a[i][j] = 2;
cin >> a[i][j];
if(m2.size() < 2) m2[a[i][j]]++;
}
if(m2.size() == 1){
cout << 1 << ' ' << 1;
return;
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j){
flag = 1;
int up = 0, down = 0;
f(i, j, up, down);
//cout << up << ' ' << down << endl;
if(mm[b[i][j]] == 0){
if(!down) ansup++;
if(!up) ansdown++;
}
mm[b[i][j]] = 1;
}
cout << ansup << ' ' << ansdown << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}