• 문제 링크
18422번: Emacs
While playing in his favourite text editor, Daniel decided to draw a picture that was N characters high and M characters wide. The picture consists solely of characters ’.’ and ’*’ such that characters ’*’ form some non-overlapping rectangles.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static int n, m;
static char[][] mat;
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
mat = new char[n][m];
for (int i = 0; i < n; i++) mat[i] = br.readLine().toCharArray();
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == '*') {
dfs(i, j);
cnt++;
}
}
}
bw.write(String.valueOf(cnt));
bw.flush();
}
private static void dfs(int y, int x) {
for (int[] d : dir) {
int ny = y + d[0], nx = x + d[1];
if (ny < 0 || ny >= n || nx < 0 || nx >= m || mat[ny][nx] == '.') continue;
mat[ny][nx] = '.';
dfs(ny, nx);
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 17848 Flight Turbulence - Graph Theory / Java (0) | 2024.02.06 |
---|---|
[백준] 31217 Y - Graph Theory / Java (0) | 2024.02.05 |
[백준] 4351 Hay Points - Data Structure / Java (0) | 2024.02.03 |
[백준] 7587 Anagrams - Data Structure / Java (0) | 2024.02.02 |
[백준] 28445 알록달록 앵무새 - Data Structure / Java (0) | 2024.02.01 |
댓글