输入一个n*n的黑白图像,统计其中黑色八连块的个数,0代表白,1代表黑。
样例输入:
6
1 0 0 1 0 0
0 0 1 0 1 0
0 0 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
0 1 0 1 0 0
样例输出:
3
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void dfs(int [][]arr,int [][]visited,int x,int y){
if(arr[x][y]==0||visited[x][y]==1)
return;
visited[x][y]=1;
dfs(arr,visited,x,y-1);
dfs(arr,visited,x,y+1);
dfs(arr,visited,x-1,y-1);
dfs(arr,visited,x-1,y);
dfs(arr,visited,x-1,y+1);
dfs(arr,visited,x+1,y-1);
dfs(arr,visited,x+1,y);
dfs(arr,visited,x+1,y+1);
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext())
{
int n=scanner.nextInt();
int [][]arr=new int[n+2][n+2];
int [][]visited=new int[n+2][n+2];