코딩테스트/baekjoon
[beakjoon] 드래곤 커브 JAVA
개발냐옹발
2022. 11. 10. 18:02
https://www.acmicpc.net/problem/15685
15685번: 드래곤 커브
첫째 줄에 드래곤 커브의 개수 N(1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 드래곤 커브의 정보가 주어진다. 드래곤 커브의 정보는 네 정수 x, y, d, g로 이루어져 있다. x와 y는 드래곤 커
www.acmicpc.net
* 먼저 문제를 이해하고, 하나하나 풀어나가자!!
1. 시작방향, 세대 수를 이용하여 좌표 회전을 ArrayList<Integer> 을 담는다.
2. 1에서 만든 arrayList를 사용하여 (x,y) 의 이동구간을 true로 변경
3. 커브가 모두 완료되면 (x, y), (x+1, y), (x, y+1), (x+1, y+1)은 정사각을 의미. 이 좌표 모두 true이면 cnt++;
package beajoon.day04;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test04 {
static int length = 101;
static boolean[][] map = new boolean[101][101];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
while(N --> 0) {
int x = sc.nextInt();
int y = sc.nextInt();
int d = sc.nextInt(); //방향
int g = sc.nextInt(); //세대
draw(x, y, getDirections(d, g));
}
System.out.println(getNumberOfSquares());
}
private static int getNumberOfSquares() {
int cnt = 0;
for(int x = 0; x < length-1; x++) {
for(int y = 0; y < length-1; y++) {
if(map[x][y] && map[x+1][y] && map[x][y+1] && map[x+1][y+1])
cnt++;
}
}
return cnt;
}
private static List<Integer> getDirections(int d, int g) {
List<Integer> directions = new ArrayList<Integer>();
directions.add(d);
while(g --> 0) {
for(int i = directions.size() - 1; i>= 0; i--) {
int direction= (directions.get(i) + 1) % 4;
directions.add(direction);
}
}
return directions;
}
private static void draw(int x, int y, List<Integer> directions) {
map[x][y] = true;
for(int direction : directions) {
switch(direction) {
case 0 :
map[++x][y] = true;
break;
case 1 :
map[x][--y] = true;
break;
case 2 :
map[--x][y] = true;
break;
case 3 :
map[x][++y] = true;
break;
}
}
}
}
[문제풀이 참고한 글]
https://dublin-java.tistory.com/34
백준15685번: 드래곤 커브 자바 해설 (삼성 SW 역량 테스트 기출 문제)
문제 : https://www.acmicpc.net/problem/15685 정답은 맨 아래에 있습니다. 문제에서 가장 큰 힌트는 크기가 1×1인 정사각형의 네 꼭짓점이 모두 드래곤 커브의 일부인 정사각형의 개수 즉 변이 기준이 아
dublin-java.tistory.com