题目描述
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
class xy{
int x;
int y;
public xy(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
public class Main {
public static int N=1010;
public static boolean st[][]=new boolean [N][N];
public static int g[][]=new int [N][N];
public static int n;
public static boolean has_higher=false,has_lower=false;
public static void bfs(int x,int y) {
has_higher=false;
has_lower=false;
Queue<xy> queue =new LinkedList<xy>();
queue.add(new xy(x, y));
st[x][y]=true;
while(!queue.isEmpty()){
xy xy=queue.poll();
for(int i=xy.x-1;i<=xy.x+1;i++){
for(int j=xy.y-1;j<=xy.y+1;j++){
if(i==xy.x && j==xy.y) continue;
if(i>=n || j>=n || i<0 || j<0) continue;
// if(st[i][j])continue;
/*if(g[i][j]>g[xy.x][xy.y]){
has_higher=true;
}
if(g[i][j]<g[xy.x][xy.y]){
has_lower=true;
}
if(!st[i][j] && g[i][j]==g[xy.x][xy.y]){
queue.add(new xy(i, j));
st[i][j]=true;
}*/
if(g[i][j]!=g[xy.x][xy.y]){
if(g[i][j]>g[xy.x][xy.y]){
has_higher=true;
}
if(g[i][j]<g[xy.x][xy.y]){
has_lower=true;
}
}
else if(!st[i][j]){
queue.add(new xy(i, j));
st[i][j]=true;
}
}
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(buff.readLine());
for(int i=0;i<n;i++){
String p[]=buff.readLine().split(" ");
for(int j=0;j<n;j++){
g[i][j]=Integer.parseInt(p[j]);
}
}
int sf=0;
int sg=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(!st[i][j]){
bfs(i, j);
if(!has_higher) sf++;
if(!has_lower)sg++;
}
}
}
System.out.println(sf+" "+sg);
}
}