본문 바로가기

알고리즘/백준 (JAVA)

백준 3109 빵집 / java 자바

www.acmicpc.net/problem/3109

 

3109번: 빵집

유명한 제빵사 김원웅은 빵집을 운영하고 있다. 원웅이의 빵집은 글로벌 재정 위기를 피해가지 못했고, 결국 심각한 재정 위기에 빠졌다. 원웅이는 지출을 줄이고자 여기저기 지출을 살펴보던

www.acmicpc.net

 

import java.util.*;
import java.io.*;

public class Main_빵집 {
	static int R, C, result;
	static boolean tf;
	static char[][] map;
	static boolean[][] ck;
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		R = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		map = new char[R][];
		ck = new boolean[R][C];
		for (int i = 0; i < R; i++) {
			map[i] = br.readLine().toCharArray();
		}
		for (int i = 0; i < R; i++) {
			tf = false;
			ck[i][0] = true;
			solve(i, 0);
		}
		System.out.println(result);
	}
	static int[] dx = {-1, 0, 1};
	static int[] dy = {1, 1, 1};
	static void solve(int x, int y) {
		if (y == C-1) {
			tf = true;
			result++;
			return;
		}
		for (int i = 0; i < 3; i++) {
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx < 0 || xx >= R || yy < 0 || yy >= C) continue;
			if (ck[xx][yy] || map[xx][yy] == 'x') continue;
			ck[xx][yy] = true;
			solve(xx, yy);
			if (tf) return;
		}
	}
}