https://www.acmicpc.net/problem/15685

 

15685번: 드래곤 커브

첫째 줄에 드래곤 커브의 개수 N(1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 드래곤 커브의 정보가 주어진다. 드래곤 커브의 정보는 네 정수 x, y, d, g로 이루어져 있다. x와 y는 드래곤 커

www.acmicpc.net

 

 

package Main;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static int N;
//x : 드래곤커브의 시작 x좌표 y:드래곤커브의 시작 y좌표 d:드래곤커브의 시작방향 g:세대
public static int x,y,d,g;
public static boolean[][] map = new boolean[101][101];
//동,북,서,남 의 방향순서입니다.
public static int[] dx = {1,0,-1,0};
public static int[] dy = {0,-1,0,1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N=sc.nextInt();
for(int i=0;i<N;i++) {
x=sc.nextInt();
y=sc.nextInt();
d=sc.nextInt();
g=sc.nextInt();
draw(x,y,dragonCurve(d,g));
}
System.out.println(getNumberofSquares());
}
public static List<Integer> dragonCurve(int d, int g) {
List<Integer> directions = new ArrayList<>();
directions.add(d);
for(int i=0;i<g;i++) {
for(int j=directions.size()-1;j>=0;j--) {
int direction = (directions.get(j)+1)%4;
directions.add(direction);
}
}
return directions;
}
public static void draw(int x, int y, List<Integer> directions) {
map[x][y] = true;
for(int i=0;i<directions.size();i++) {
switch(directions.get(i)) {
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;
}
}
}
public static int getNumberofSquares() {
int count=0;
for(int x=0;x<100;x++) {
for(int y=0;y<100;y++) {
if(map[x][y] == true && map[x+1][y] == true &&map[x][y+1] == true &map[x+1][y+1] == true) {
count+=1;
}
}
}
return count;
}
}

 

이번문제에서 신기했던점은 

0

0 1

0 1 2 1

0 1 2 1 2 3 2 1

 

이런식으로 규칙성을 찾는것과 이것들을 뒤에서부터 더해가며 만들었다는 점입니다.

또한 마지막에 getNumberOfSquares에서 x+1이므로 <=100 이 아니라 <100입니다. 좋은문제인것같습니다.

+ Recent posts