티스토리 뷰
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 배열을 1번 인덱스부터 6번 인덱스까지 각각 바닥 , 오른쪽 , 앞쪽 , 뒤쪽 ,왼쪽 , 위쪽 으로 설정했다.
전체 코드
import java.util.*;
import java.io.*;
public class Main {
static int n = 0;
static int m = 0;
static int x = 0;
static int y = 0;
static int k = 0;
static int[][] board;
static int[] dx = {0,0,0,-1,1};
static int[] dy = {0,1,-1,0,0};
static int[] dice = new int[7];
public static void main(String[] args) throws IOException {
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());
board = new int[n+1][m+1];
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine(), " ");
for(int j=0;j<m;j++){
board[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine(), " ");
for(int i=1;i<=k;i++){
int num = Integer.parseInt(st.nextToken());
move(num);
}
br.close();
}
static void move(int dir){
int nx = x + dx[dir];
int ny = y + dy[dir];
if(nx < 0 || ny < 0 || nx >= n || ny >= m)
return;
x = nx;
y = ny;
if(dir == 1){
R(x,y);
}
else if(dir == 2){
L(x,y);
}
else if(dir == 3){
U(x,y);
}
else{
D(x,y);
}
System.out.println(dice[6]);
}
static void R(int x,int y){
if(board[x][y] == 0){
board[x][y] = dice[2];
}
else{
dice[2] = board[x][y];
board[x][y] = 0;
}
int[] tmp = new int[7];
tmp[5] = dice[1];
tmp[2] = dice[6];
tmp[1] = dice[2];
tmp[6] = dice[5];
tmp[3] = dice[3];
tmp[4] = dice[4];
dice = tmp;
}
static void L(int x,int y){
if(board[x][y] == 0){
board[x][y] = dice[5];
}
else{
dice[5] = board[x][y];
board[x][y] = 0;
}
int[] tmp = new int[7];
tmp[5] = dice[6];
tmp[2] = dice[1];
tmp[1] = dice[5];
tmp[6] = dice[2];
tmp[3] = dice[3];
tmp[4] = dice[4];
dice = tmp;
}
static void U(int x,int y){
if(board[x][y] == 0){
board[x][y] = dice[3];
}
else{
dice[3] = board[x][y];
board[x][y] = 0;
}
int[] tmp = new int[7];
tmp[3] = dice[6];
tmp[4] = dice[1];
tmp[2] = dice[2];
tmp[5] = dice[5];
tmp[1] = dice[3];
tmp[6] = dice[4];
dice = tmp;
}
static void D(int x,int y){
if(board[x][y] == 0){
board[x][y] = dice[4];
}
else{
dice[4] = board[x][y];
board[x][y] = 0;
}
int[] tmp = new int[7];
tmp[4] = dice[6];
tmp[3] = dice[1];
tmp[2] = dice[2];
tmp[5] = dice[5];
tmp[1] = dice[4];
tmp[6] = dice[3];
dice = tmp;
}
}'알고리즘 > 백준' 카테고리의 다른 글
| 백준 5582 (공통 부분 문자열) - java (0) | 2023.09.10 |
|---|---|
| 백준 10021 (Watering the Fields) - java (0) | 2023.09.10 |
| 백준 10653 (마라톤 2) - java (0) | 2023.09.07 |
| 백준 17780 (새로운 게임) - java (0) | 2023.09.05 |
| 백준 11054 (가장 긴 바이토닉 부분 수열) - java (0) | 2023.09.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 올해보다
- 백준 #다리 만들기 #2146
- 백준 #17940 #주식 #자바 #java
- 백준 #인구 이동 #16234
- 백준 #3980 #선발 명단
- 자바
- 17394
- 백준 #28140 #빨강~ 빨강~ 파랑! 파랑! 달콤한 솜사탕! #java #자바
- 백준
- 백준 #2636 #치즈
- 백준 #12014 #주식 #자바 #java
- 백준 #1759 #암호 만들기
- 백준 #14863 #서울에서 경산까지 #java #자바
- 백준 #25603 #짱해커 이동식 #java #자바
- 백준 #2580 #스도쿠
- 백준 #치즈 #2638
- 백준 #15686 #치킨 배달
- Java
- 백준 #13549 #숨바꼭질3
- 백준 #4963 #섬의 개수
- 백준 #1987 #알파벳 #자바 #java
- 백준 #1325 #효율적인 해킹
- 백준 #
- 백준 #18405 #경쟁적 전염
- 자바 #JAVA
- 백준 #1584 #게임 #java #자바
- 행복합시다.
- 백준 #5721 #사탕 줍기 대회 #java #자바
- 백준 #16973 #직사각형 탈출
- 17218
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함