Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[SW Expert Academy] 2819. 격자판의 숫자 이어 붙이기 본문

SW_Expert

[SW Expert Academy] 2819. 격자판의 숫자 이어 붙이기

코드와이 2021. 4. 6. 16:01

 

문제링크

swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=4&contestProbId=AV7I5fgqEogDFAXB&categoryId=AV7I5fgqEogDFAXB&categoryType=CODE&problemTitle=&orderBy=PASS_RATE&selectCodeLang=JAVA&select-1=4&pageSize=10&pageIndex=4

 

SW Expert Academy

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

swexpertacademy.com

 

package D4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 격자판_숫자_이어_붙이기 {

	static int[][] arr;
	static boolean[] visited;
	static int ans;
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		
		for(int tc = 1 ; tc <= T ; tc++) {
			sb.append("#").append(tc).append(" ");
			
			arr = new int[4][4];
			for(int i = 0 ; i < 4 ; i++) {
				st = new StringTokenizer(br.readLine());
				for(int j = 0 ; j < 4 ; j++) {
					arr[i][j] = Integer.parseInt(st.nextToken());
				}
			}
			
			visited = new boolean[10000000];
			ans = 0;
			
			for(int i = 0 ; i < 4 ; i++) {
				for(int j = 0 ; j < 4 ; j++) {
					go(i, j, 1, Integer.toString(arr[i][j]));
				}
			}
			
			sb.append(ans).append("\n");
		}
		sb.setLength(sb.length() - 1);
		System.out.println(sb);
	}
	
	static int[] dr = {-1, 1, 0, 0};
	static int[] dc = {0, 0, 1, -1};
	private static void go(int r, int c, int cnt, String str) {
		
		if(cnt == 7) {
			int x = Integer.parseInt(str);
			
			if(!visited[x]) {
				visited[x] = true;
				ans++;
			}
			return;
		}
		
		for(int d = 0 ; d < 4 ; d++) {
			int nr = r + dr[d];
			int nc = c + dc[d];
			
			if(check(nr, nc)) {
				go(nr, nc, cnt+1, str + Integer.toString(arr[nr][nc]));
			}
		}
	}
	
	private static boolean check(int r, int c) {
		if(r < 0 || c < 0 || r > 3 || c > 3) return false;
		return true;
	}
	
}