https://www.acmicpc.net/problem/15685
* 먼저 문제를 이해하고, 하나하나 풀어나가자!!
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
'코딩테스트 > baekjoon' 카테고리의 다른 글
[baekjoon]회의실 배정 JAVA (0) | 2022.11.24 |
---|---|
[baekjoon] 로봇 청소기 JAVA (0) | 2022.11.09 |
[beakjoon]퇴사 JAVA (0) | 2022.11.08 |
[baekjoon] 주사위 굴리기 JAVA (0) | 2022.11.08 |