-
백준 15685번 드래곤 커브(자바) - 삼성 sw 역량인증코딩테스트 2025. 6. 5. 17:25
문제 풀이
1. List에 현재 드래곤 커브의 각 변마다의 방향 정보를 모두 저장한다.
2. 끝점을 기준으로 List를 끝에서부터 탐색하며 방향값 + 1(왼쪽으로 90도 회전한 방향) 으로 끝점을 이동시킨 후
visited 체크를 해준다
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Main { static int[][] map; static int[] dx = {1,0,-1,0}; static int[] dy = {0,-1,0,1}; public static void main (String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); map = new int[101][101]; for (int i=0; i<n; i++) { draw(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); } check(); } static void draw(int x, int y, int d, int g) { List<Integer> dragon = new ArrayList<>(); //0세대 드래곤 커브 정보 저장 dragon.add(d); map[x][y] = 1; int ex = x + dx[d]; int ey = y + dy[d]; map[ex][ey] = 1; //g세대 드래곤 커브 for (int i=0; i<g; i++) { int size = dragon.size(); for (int j=size-1; j>=0; j--) { //list를 역순으로 조회하며 int ndir = dragon.get(j) + 1; if (ndir > 3) ndir = 0; ex += dx[ndir]; ey += dy[ndir]; //왼쪽 90도 회전한 방향으로 끝점을 이동시켜줌 map[ex][ey] = 1; dragon.add(ndir); } } } //정사각형 개수 체크 static void check() { int cnt = 0; for (int i=0; i<100; i++) { for (int j=0; j<100; j++) { if (map[i][j]==1 && map[i+1][j]==1 && map[i][j+1]==1 && map[i+1][j+1]==1) cnt++; } } System.out.println(cnt); } }
'코딩테스트' 카테고리의 다른 글
백준 14501번 퇴사(자바) - 삼성 sw 역량인증, DP (1) 2025.06.09 백준 14500 테트로미노(자바) - 삼성 sw 역량 인증 (0) 2025.06.09 백준 12100 번 2048(자바) - 삼성sw역량 기출 (0) 2025.06.04 백준 1197 MST - 크루스칼 알고리즘 (0) 2025.06.01 백준 13460번 구슬 탈출2 (0) 2025.05.29