백준 격자상의 경로 - 10164 [JAVA] - 23년 1월 1일 - DFS

Denia·2023년 1월 1일
0

코딩테스트 준비

목록 보기
125/201
post-custom-banner

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static public void main(String[] args) throws IOException {
        BjSolution sol = new BjSolution();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int rows = Integer.parseInt(line[0]);
        int cols = Integer.parseInt(line[1]);
        int specialNum = Integer.parseInt(line[2]);

        sol.solution(rows, cols, specialNum);
    }
}

class BjSolution {
    int answer, gRows, gCols, gSpecialNum;
    int[][] directions = {{0, 1}, {1, 0}};
    int[][] grid;

    public void solution(int rows, int cols, int specialNum) {
        answer = 0;
        gRows = rows;
        gCols = cols;
        gSpecialNum = specialNum;

        grid = new int[rows][cols];

        int tempVal = 1;
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                grid[r][c] = tempVal;
                tempVal++;
            }
        }

        dfs(0, 0, false);

        System.out.println(answer);
    }

    private void dfs(int r, int c, boolean checkFlag) {
        if (r == gRows - 1 && c == gCols - 1) {
            if (gSpecialNum != 0) {
                if (checkFlag)
                    answer++;
            } else {
                answer++;
            }
            return;
        }

        for (int[] direction : directions) {
            int nr = r + direction[0];
            int nc = c + direction[1];
            if (nr >= 0 && nr < gRows && nc >= 0 && nc < gCols) {
                boolean tempCheckFlag = checkFlag || grid[nr][nc] == gSpecialNum;
                dfs(nr, nc, tempCheckFlag);
            }
        }
    }
}

profile
HW -> FW -> Web
post-custom-banner

0개의 댓글