package 신규아이디추천;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static char[][] map;
static void DFS(char condition, int r, int c, boolean[][] visited) {
if (r < 0 || r >= map.length)
return;
if (c < 0 || c >= map[0].length)
return;
if (map[r][c] != condition)
return;
if (visited[r][c])
return;
visited[r][c] = true;
DFS(condition, r - 1, c, visited);
DFS(condition, r + 1, c, visited);
DFS(condition, r, c - 1, visited);
DFS(condition, r, c + 1, visited);
DFS(condition, r - 1, c - 1, visited);
DFS(condition, r + 1, c + 1, visited);
DFS(condition, r + 1, c - 1, visited);
DFS(condition, r - 1, c + 1, visited);
}
static int countMap() {
boolean[][] visited = new boolean[map.length][map[0].length];
int count = 0;
for (int r = 0; r < map.length; ++r)
for (int c = 0; c < map[0].length; ++c)
if (visited[r][c] == false) {
char condition = map[r][c];
if (condition == '1')
++count;
DFS(condition, r, c, visited);
}
return count;
}
static void countMapList(ArrayList<char[][]> map_list) {
for (int r = 0; r < map_list.size(); ++r) {
map = map_list.get(r);
System.out.println(countMap());
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder builder = new StringBuilder();
int K = -1;
int N = -1;
ArrayList<char[][]> map_list = new ArrayList<char[][]>();
char[][] temp_map;
while (true) {
StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
N = Integer.parseInt(tokenizer.nextToken());
K = Integer.parseInt(tokenizer.nextToken());
temp_map = new char[K][];
if (K == 0 && N == 0)
break;
for (int i = 0; i < K; ++i)
temp_map[i] = reader.readLine().replaceAll(" ", "").toCharArray();
map_list.add(temp_map);
}
countMapList(map_list);
}
}
처음에 map 배열 입력 받을려고 했을때
temp_map[i] = reader.readLine().toCharArray();
요런 식으로 해서 값이 제대로 입력이 안됨
왜냐하면 "1 1 0 0"을 입력할때 " "공백 때문에 제대로 입력이 안된다.
temp_map[i] = reader.readLine().replaceAll(" ", "").toCharArray();
그래서 요런식으로 공백을 없애준다.