蒜头君和他的朋友周末相约去召唤师峡谷踏青。他们发现召唤师峡谷的地图是由一块一块格子组成的,有的格子上是草丛,有的是空地。草丛通过上下左右 4 个方向扩展其他草丛形成一片草地,任何一片草地中的格子都是草丛,并且所有格子之间都能通过上下左右连通。如果用'#'
代表草丛,'.'
代表空地,下面的峡谷中有 2 片草地。
处在同一个草地的 2 个人可以相互看到,空地看不到草地里面的人。他们发现有一个朋友不见了,现在需要分头去找,每个人负责一片草地,蒜头君想知道他们至少需要多少人。
输入格式
第一行输入 n, m (1≤n,m≤100) 表示峡谷大小
接下来输入 n 行字符串表示峡谷的地形
输入格式
输出至少需要多少人
样例输入
5 6 .#.... ..#... ..#..# ...##. .#....
样例输出
5
import java.util.Scanner;
public class Main {
static String[] a = new String[101];
static int n,m;
static boolean[][] vis = new boolean[100][100];
static int[][] dir={{1,0},{0,1},{-1,0},{0,-1}};
private static void dfs(int x, int y) {
vis[x][y]=true;
for(int i=0;i<4;i++){
x+=dir[i][0];
y+=dir[i][1];
if ((x < n && x > -1) && (y < m && y > -1))
//艹!打反变量!!!!!
if ('#'==a[x].charAt(y) && false==vis[x][y] ) {
dfs(x,y);
}
x-=dir[i][0];
y-=dir[i][1];
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
int ans = 0;
for(int i=0;i<n;i++)
a[i]=sc.next();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(('#' ==a[i].charAt(j)) && (vis[i][j]==false)){
vis[i][j]=true;
ans++;
dfs(i,j);
}
}
System.out.println(ans);
}
}