时间限制:
10000ms
单点时限:
1000ms
内存限制:
256MB
-
3 4 /\/\ \ / \/\
样例输出
-
7
描述
小Hi有一块由NxM个单位正方形组成的矩形。现在小Ho在某些单位正方形上画了一道分割线,这条分割线或者是单位正方形的主对角线(用'\'表示),或者是副对角线(用'/'表示)。
现在小Hi想知道这些分割线把NxM的矩形分割成了多少块区域。
例如
/\ \/
就把2x2的矩形分成了5个区域。
/\/\ \ / \/\
把3x4的矩形分成了7个区域。
输入
第一包含两个整数N和M。(1 <= N, M <= 100)
以下N行每行包含M个字符。每个字符只可能是/\或者空格。
输出
分割成的区域数目。
/**
* Created by HuLihong on 2017/4/5.
*/
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while(in.hasNext()){
String aaa=in.nextLine();
String[] words=aaa.split(" ");
int n=Integer.valueOf(words[0]);
int m=Integer.valueOf(words[1]);
int[][] table=new int[2*n][2*m];
for(int i=0;i<n;i++){
String str=in.nextLine().toString();
for(int j=0;j<str.length();j++){
char temp=str.charAt(j);
if(temp=='/'){
table[2*i][2*j+1]=-1;
table[2*i+1][2*j]=-1;
}else if(temp==' '){
}else {
table[2*i][2*j]=-1;
table[2*i+1][2*j+1]=-1;
}
}
}
int count=0;
for(int i=0;i<table.length;i++){
for(int j=0;j<table[0].length;j++){
if(table[i][j]==0){
count++;
table[i][j]=count;
help(i,j,table,count);
}
}
}
//
// for(int i=0;i<table.length;i++){
// for(int j=0;j<table[0].length;j++){
// System.out.print(table[i][j]);
// System.out.print(" ");
// }
// System.out.print("\n");
// }
System.out.print(count);
}
}
private static void help(int i,int j,int[][] M,int count){
if(i-1>=0&&M[i-1][j]==0){
M[i-1][j]=count;
help(i-1,j,M,count);
}
if(j-1>=0&&M[i][j-1]==0){
M[i][j-1]=count;
help(i,j-1,M,count);
}
if(i+1<M.length&&M[i+1][j]==0){
M[i+1][j]=count;
help(i+1,j,M,count);
}
if(j+1<M[0].length&&M[i][j+1]==0){
M[i][j+1]=count;
help(i,j+1,M,count);
}
}
}