백준

백준 2210 숫자판 점프(Java)

Park DJ 2023. 2. 11. 15:46

https://www.acmicpc.net/problem/2210

 

2210번: 숫자판 점프

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.

www.acmicpc.net


 

Java 코드

 


import java.util.Scanner;
import java.util.HashSet;

public class Main {

  static HashSet<String> set = new HashSet<>();
  static int[][] arr = new int[5][5];
  static int[] dx = {1, -1, 0, 0};
  static int[] dy = {0, 0, -1, 1};
  
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String s = "";

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

    for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) dfs(i, j, 0 ,s);
    }

    System.out.print(set.size());
  }

  public static void dfs(int a, int b, int dep, String str) {
    if(dep == 6) {
      set.add(str);
      return;
    }
    
    for(int i = 0; i < 4; i++) {
      int x = a + dx[i];
      int y = b + dy[i];
      if((x >= 0 && x < 5) && (y >= 0 && y < 5)){
        dfs(x, y, dep + 1, str + arr[x][y]);
      }
    }
  }

}