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]);
}
}
}
}
'백준' 카테고리의 다른 글
백준 24479 알고리즘 수업(Java) (0) | 2023.02.12 |
---|---|
백준 15665 N과 M (11)(Java) (0) | 2023.02.11 |
백준 1138 한 줄로 서기(Java) (0) | 2023.02.11 |
백준 11060 점프 점프(Java) (0) | 2023.02.11 |
백준 15990 1, 2, 3 더하기 5(Java) (0) | 2023.02.11 |