728x90
300x250
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
'알고리즘 > DFS & BFS' 카테고리의 다른 글
백준 1303번: 전쟁 - 전투 [DFS][BFS][Java] (1) | 2021.04.14 |
---|---|
백준 17143번: 낚시왕 (0) | 2020.08.10 |
SWEA - 1226. [S/W 문제해결 기본] 7일차 - 미로1 (0) | 2020.08.09 |
SWEA - 1210. [S/W 문제해결 기본] 2일차 - Ladder1 (0) | 2020.08.09 |
프로그래머스[Level2] - 카카오프렌즈 컬러링북 [2017 카카오코드 예선] (0) | 2020.04.04 |