241217 빵집

Jongleee·2024년 12월 17일
0

TIL

목록 보기
758/786
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int rows, cols;
private static char[][] grid;

public static void main(String[] args) throws Exception {
	StringTokenizer st = new StringTokenizer(br.readLine());
	rows = Integer.parseInt(st.nextToken());
	cols = Integer.parseInt(st.nextToken());

	grid = new char[rows][];
	for (int i = 0; i < rows; i++) {
		grid[i] = br.readLine().toCharArray();
	}

	System.out.println(countPipelines());
}

private static int countPipelines() {
	int pipelineCount = 0;

	for (int r = 0; r < rows; r++) {
		if (grid[r][0] == '.') {
			if (dfs(r, 0)) {
				pipelineCount++;
			}
		}
	}

	return pipelineCount;
}

private static boolean dfs(int r, int c) {
	if (c == cols - 1)
		return true;
	grid[r][c] = 'x';

	return (isValid(r - 1, c + 1) && dfs(r - 1, c + 1)) ||
			(isValid(r, c + 1) && dfs(r, c + 1)) ||
			(isValid(r + 1, c + 1) && dfs(r + 1, c + 1));
}

private static boolean isValid(int r, int c) {
	return r >= 0 && r < rows && grid[r][c] == '.';
}

출처:https://www.acmicpc.net/problem/3109

0개의 댓글