请问下大家,第二题这样什么只能过80%,提示数组越界?不可能啊
import java.util.Scanner;
/**
* Created by forcht on 2018/8/22.
*/
public class Main {
static int[] xt={0,0,-1,1};
static int[] yt={-1,1,0,0};
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
if(n<=0)return;
int[][] map=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
map[i][j]=scanner.nextInt();
}
}
int count=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(map[i][j]==1){
dfs(map,i,j,n);
count++;
}
}
}
System.out.println(count);
}
static void dfs(int[][] map,int x,int y,int n){
if(x<0||x>=n||y<0||y>=n||map[x][y]==0)return;
map[x][y]=0;
for(int i=0;i<4;i++){
dfs(map,x+xt[i],y+yt[i],n);
}
}
}