반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 시뮬레이션
- sql태그
- ClassPathResource
- 주사위굴리기
- 차이점
- 참조타입
- 내맘대로정리
- Exception
- 코딩테스트
- 원시타입
- mybatis
- JSON
- Char[]
- 알고리즘
- 백준
- Spring
- include태그
- 데이터탑입
- SQL
- 변수
- 차이
- char
- string
- json파싱
- java
- 프로그래머스
- 자바오류
- ReferenceType
- 자바
- primitivetype
Archives
- Today
- Total
재채기는 H
백준 14499 - 주사위 굴리기 JAVA 본문
반응형
https://www.acmicpc.net/problem/14499
14499번: 주사위 굴리기
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도
www.acmicpc.net
전형적인 시뮬레이션 문제
어렵지 않았다. 그냥 문제에서 알려주는 조건에 따라 움직이면 된다.
나는 좀더 편하게 할려고 Dice라는 주사위 클래스를 만들었다.
X,Y는 주사위의 맵에서의 위치를 말해주고
top, right, up 은 주사위의 세개의 면을 말해준다.
어차피 주사위는 세개의 면에서 7을 빼주면 마주보는 면을 구할 수 있기때문에 세면만 이용했다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
class Dice {
int x, y;
int top;
int up;
int rigth;
Dice(int x, int y, int top, int up, int right) {
this.x = x;
this.y = y;
this.top = top;
this.up = up;
this.rigth = right;
}
}
public class 백준_주사위굴리기_14499 {
static int N, M, x, y, K;
static int[][] map;
static int[] diceArr = new int[7];
static Queue<Integer> qu;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
qu = new LinkedList<>();
for (int i = 0; i < K; i++) {
qu.add(Integer.parseInt(st.nextToken()));
}
Dice dice = new Dice(x, y, 1, 2, 3);
while (!qu.isEmpty()) {
int n = qu.poll();
int temp;
switch (n) {
case 1: // 동
if (0 <= dice.y + 1 && dice.y + 1 < M) {
dice.y += 1;
}else {
continue;
}
temp = dice.top;
dice.top = 7 - dice.rigth;
dice.rigth = temp;
break;
case 2: // 서
if (0 <= dice.y - 1 && dice.y - 1 < M) {
dice.y -= 1;
}else {
continue;
}
temp = dice.top;
dice.top = dice.rigth;
dice.rigth = 7 - temp;
break;
case 3: // 북
if (0 <= dice.x - 1 && dice.x - 1 < N) {
dice.x -= 1;
}else {
continue;
}
temp = dice.up;
dice.up = dice.top;
dice.top = 7 - temp;
break;
case 4: // 남
if (0 <= dice.x + 1 && dice.x + 1 < N) {
dice.x += 1;
}else {
continue;
}
temp = dice.top;
dice.top = dice.up;
dice.up = 7 - temp;
break;
}
if(map[dice.x][dice.y]!=0) {
diceArr[7-dice.top]=map[dice.x][dice.y];
map[dice.x][dice.y]=0;
}else {
map[dice.x][dice.y]=diceArr[7-dice.top];
}
System.out.println(diceArr[dice.top]);
}
}
}
반응형
'알고리즘' 카테고리의 다른 글
백준 14503 - 로봇 청소기 JAVA (0) | 2020.07.22 |
---|---|
백준 14502 - 연구소 JAVA (0) | 2020.07.22 |
백준 12100 - 2048 (Easy) JAVA (0) | 2020.07.20 |
백준 13458 - 시험 감독 JAVA (0) | 2020.07.20 |
백준 3190 - 뱀 JAVA (0) | 2020.07.17 |
Comments