SWEA - 2819. 격자판의 숫자 이어 붙이기 [D4] :: 매운코딩
728x90
300x250

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV7I5fgqEogDFAXB&categoryId=AV7I5fgqEogDFAXB&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

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

public class Solution {

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

		T =sc.nextInt();
		
		for (int tc = 0; tc < T; tc++) {
			
			set.clear();
			map = new int[4][4];
			
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					map[i][j]=sc.nextInt();
				}
			}
			
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					dfs(i,j,"");
				}
			}
			
			
			//System.out.println(set);
			System.out.println("#"+(tc+1)+" "+set.size());
		}
	}
	
	public static void dfs(int x, int y, String str) {
		if(str.length()==7) {
			set.add(str);
			return;
		}
		
		for (int i = 0; i < dx.length; i++) {
			int nx = x+dx[i];
			int ny = y+dy[i];
			
			//범위를 벗어나는가?
			if( nx>=4 || nx <0 || ny >= 4 || ny < 0)
				continue;
			
			dfs(nx,ny,str+map[nx][ny]);
		}
	}

}
728x90

+ Recent posts