Algorithm/baekjoon

숫자판 점프

마닐라 2022. 1. 22. 23:44

📍 문제 설명

💡 접근

이동한 횟수, 붙인 문자열을 인자로 DFS 수행해주면 된다.

 

👩‍💻 코드

import java.util.*;

public class Main {
    static int n, m, h, k, answer,cnt, max, L, R, C;
    static int[][] board, dis;
    static boolean[][][][] visited;
    static int[] ch,pm, combi;
    static boolean flag = true;
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,-1,1};
    static HashSet<String> set = new HashSet<>();
    public static void main(String[] args) {
        Main T = new Main();
        Scanner kb = new Scanner(System.in);

        board = new int[5][5];

        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
                board[i][j] = kb.nextInt();
            }
        }

        String s = "";

        //임의의 위치에서 시작
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
                //좌표, 더한 문자열의 갯수, 더한 문자열
                T.solution(i, j, 0, s);
            }
        }
        System.out.println(set.size());

    }

    private void solution(int x, int y, int count, String s) {
        if(count == 6) {
            set.add(s);
            return;
        }
        else {
            //네 방향으로 이동
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && ny >= 0 && nx < 5 && ny < 5) {
                    solution(nx, ny, count+1, s+board[x][y]);
                }
            }
        }
    }
}

'Algorithm > baekjoon' 카테고리의 다른 글

연구소  (0) 2022.01.23
종이의 개수  (0) 2022.01.23
숨바꼭질4  (0) 2022.01.22
숨바꼭질2  (0) 2022.01.22
구슬 탈출  (0) 2022.01.22