CS/Algorithm

[백준:2210] 숫자판 점프 - JAVA

yujindonut 2021. 4. 11. 15:16
728x90

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

public class Main {

	static int[] dx = {-1,1,0,0};
	static int[] dy = {0,0,-1,1};
	static int[][] arr;
	static HashSet<String> list;
	static int N;
	
	public static void main(String[] args) throws Exception {
		
		Scanner scan = new Scanner(System.in);
		
		N = 5;
		list = new HashSet<String>();
		arr = new int[N][N];
		
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N; j++) {
				arr[i][j] = scan.nextInt();
			}
		}
		String s = new String();
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N; j++) {
				DFS(i,j,0,s);
			}
		}
		System.out.println(list.size());
		scan.close();
	}
	public static void DFS(int x, int y, int depth, String s) {
		
//		길이가 6일때 set에 넣고 종료한다.
		if(depth == 6) {
			list.add(s);
			return;
		}
		
//		상하좌우 이동할 수 있다. 왔던 길도 다시 올 수 있다.
		for(int i = 0; i < 4; i++) {
			int nextX = dx[i] + x;
			int nextY = dy[i] + y;
			
			if(nextX < 0 || nextY < 0 || nextX >= N || nextY >= N)
				continue;
			
			DFS(nextX, nextY, depth + 1, s + arr[x][y]);
		}
	}
}
728x90