티스토리 뷰
15685번: 드래곤 커브
첫째 줄에 드래곤 커브의 개수 N(1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 드래곤 커브의 정보가 주어진다. 드래곤 커브의 정보는 네 정수 x, y, d, g로 이루어져 있다. x와 y는 드래곤 커
www.acmicpc.net
풀이
1) 좌표, 시작방향, 세대 정보를 담을 클래스를 만든다
2) 모든 방향을 리스트에 담음
3) 방향이 담긴 리스트로 맵을 그린다
4) 네모를 찾는다.
주의사항
1) 방향을 담을때 반대로 담아야함 (직접 해봐야 암)
2) 리스트를 만들땐 스택을 이용해야함
package com.BackJoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
public class BJ_G4_15685_드래곤커브 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int N,map[][] = new int[101][101], answer;
static List<Node> list = new ArrayList<>();
public static void main(String[] args) throws NumberFormatException, IOException {
//input = new BufferedReader(new StringReader(src));
N = Integer.parseInt(input.readLine());
for(int n=0; n<N; n++) {
tokens = new StringTokenizer(input.readLine());
int x = Integer.parseInt(tokens.nextToken());
int y = Integer.parseInt(tokens.nextToken());
int d = Integer.parseInt(tokens.nextToken());
int g = Integer.parseInt(tokens.nextToken());
dirStore(y,x,d,g);
}
getSquare();
System.out.println(answer);
}
// 모든 작업이 끝난 후 정사각형의 개수를 구하는 함수
private static void getSquare() {
for(int r=0; r<map.length-1; r++) {
for(int c=0; c<map.length-1; c++) {
if(map[r][c] == 1 && map[r][c] == map[r+1][c] && map[r][c] == map[r][c+1] && map[r][c]== map[r+1][c+1]) {
answer++;
}
}
}
}
// 방향을 리스트에 저장하는 함수
private static void dirStore(int r, int c, int d, int g) {
int cnt = 0;
List<Integer> dir = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
dir.add(d);
while(cnt < g) {
for(int i=0; i<dir.size(); i++) {
stack.push(dir.get(i));
}
while(!stack.isEmpty()) {
switch (stack.pop()) {
case 0:
dir.add(1);
break;
case 1:
dir.add(2);
break;
case 2:
dir.add(3);
break;
case 3:
dir.add(0);
break;
}
}
cnt++;
}
makeMap(r,c,dir);
}
// 방향(dir)을 받아 지도를 그리는 함수
private static void makeMap(int r, int c, List<Integer> dir) {
map[r][c] = 1;
for(int i=0; i<dir.size(); i++) {
switch (dir.get(i)) {
case 0:
c = c+1;
if(isIn(r, c)) {
}
map[r][c] = 1;
break;
case 1:
r = r-1;
if(isIn(r, c)) {
map[r][c] = 1;
}
break;
case 2:
c = c-1;
if(isIn(r, c)) {
map[r][c] = 1;
}
break;
case 3:
r = r+1;
if(isIn(r, c)) {
map[r][c] = 1;
}
break;
}
}
}
// 배열 밖으로 나가는지 검사
static boolean isIn(int r, int c) {
return (r>=0 && c>=0 && r<map.length && c<map.length);
}
static class Node{
int r;
int c;
int d;
int g;
public Node(int r, int c, int d, int g) {
super();
this.r = r;
this.c = c;
this.d = d;
this.g = g;
}
@Override
public String toString() {
return "Node [r=" + r + ", c=" + c + ", d=" + d + ", g=" + g + "]";
}
}
static String src =
"3\r\n" +
"3 3 0 1\r\n" +
"4 2 1 3\r\n" +
"4 2 2 1";
}
'Algorithm' 카테고리의 다른 글
[SWEA] D4 8382 방향 전환 (java) (0) | 2020.12.16 |
---|---|
[SWEA] D3 6808 규영이와 인영이의 카드게임 (java) (0) | 2020.12.16 |
[SWEA] D3 2814 최장 경로 (java) (0) | 2020.12.16 |
[SWEA] 1949 등산로 조성 (java) (0) | 2020.12.16 |
[SWEA] 5658 보물상자 비밀번호 (java) (0) | 2020.12.16 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- S2
- java
- 백준
- 문자열
- S3
- 구현
- 리액트
- 객체지향
- Spring Boot
- 우선순위큐
- 현꾸라지
- 그리디
- 코딩새내기
- 리액트 네이티브
- 알고리즘
- g4
- map
- Spring
- laugh4mile
- DFS
- BFS
- 자바
- G5
- react
- 시뮬레이션
- SWEA
- 다익스트라
- PriorityQueue
- react native
- 백트래킹
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함