티스토리 뷰
문제 개요
문제에서 요구하는 조건들을 만족할 수 있도록 구현하는 문제입니다.
문제 접근
하나의 세대가 증가할 때 기존 이동 경로의 2배만큼 늘어납니다. 또한 늘어나는 경로는 기존 경로의 역순으로 늘어나며, 방향은 시계방향으로 한번 회전한 방향입니다.
해당 부분만 조심해서 구현하면 쉽게 풀 수 있었습니다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Pos{
private int row;
private int col;
static final int[][] dirs = {
{0, 1},
{-1, 0},
{0, -1},
{1, 0}
};
public Pos(int row, int col){
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public void move(int dir){
row += dirs[dir][0];
col += dirs[dir][1];
}
}
public class Main {
static boolean[][] dragoncurve;
public static void main(String[] args) throws IOException {
input();
process();
}
private static void process() {
int result = 0;
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
if(dragoncurve[i][j] && dragoncurve[i][j+1] && dragoncurve[i+1][j] && dragoncurve[i+1][j+1]){
result++;
}
}
}
System.out.println(result);
}
private static boolean isIn(Pos pos){
return pos.getRow() >= 0 && pos.getRow() <= 100 && pos.getCol() >= 0 && pos.getCol() <= 100;
}
private static void check(Pos pos){
if(!isIn(pos)){
return;
}
dragoncurve[pos.getRow()][pos.getCol()] = true;
}
private static void dragon(int[] numbers){
Pos pos = new Pos(numbers[1], numbers[0]);
List<Integer> dirs = new ArrayList<>();
// make dragon curve
dirs.add(numbers[2]);
for(int j=1;j<=numbers[3];j++){
int size = dirs.size();
for(int k=size-1;k>=0;k--){
dirs.add((dirs.get(k) + 1)%4);
}
}
// check positions
check(pos);
for(int j=0;j<dirs.size();j++){
pos.move(dirs.get(j));
check(pos);
}
}
private static void input() throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
dragoncurve = new boolean[105][105];
int N = Integer.parseInt(bf.readLine());
for(int i=0;i<N;i++){
dragon(Arrays.stream(bf.readLine().split(" ")).mapToInt(Integer::parseInt).toArray());
}
}
}
문제
'문제풀이 > 백준' 카테고리의 다른 글
| 17144. 미세먼지 안녕! (1) | 2024.01.26 |
|---|---|
| 16236. 아기 상어 (1) | 2024.01.23 |
| 15684. 사다리 조작 (0) | 2024.01.18 |
| 14890. 경사로 (0) | 2024.01.17 |
| 15683. 감시 (0) | 2024.01.16 |
