문제 : 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개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 주사위를 놓은 칸에 쓰여 있는 수는 항상 0이다. 지도의 각 칸에 쓰여 있는 수는 10을 넘지 않는 자연수 또는 0이다. 마
www.acmicpc.net
문제 요약 : 주사위를 굴려 주사위 윗면의 숫자를 굴릴때 마다 출력
입력 | 출력 |
1 ≤ N,M(크기) ≤ 20 0 ≤ x ≤ N-1 0 ≤ y ≤ M-1 1 ≤ K(명령)≤ 1000 |
주사위 윗면의 수 |
JAVA
채점 번호 | 아이디 | 문제 번호 | 결과 | 메모리 | 시간 | 언어 | 코드 길이 |
6350549 | cbkpar | 14499 | 맞았습니다!! | 13960KB | 112ms | Java | 1626B |
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n,m,x,y,k,i,j;
String str = "";
str = br.readLine();
String[] arr = str.split(" ");
n = Integer.parseInt(arr[0]);
m = Integer.parseInt(arr[1]);
y = Integer.parseInt(arr[2]);
x = Integer.parseInt(arr[3]);
k = Integer.parseInt(arr[4]);
int[][] map = new int[n][m];
for(i=0;i<n;i++) {
str = br.readLine();
arr = str.split(" ");
for(j=0;j<m;j++) {
map[i][j] = Integer.parseInt(arr[j]);
}
}
int dice_l = 0; //왼쪽
int dice_r = 0; //오른쪽
int dice_u = 0; //위
int dice_d = 0; //아래
int dice_f = 0; //앞
int dice_b = 0; //뒤
int dice_t;
str = br.readLine();
arr = str.split(" ");
for(i=0;i<k;i++) {
if(Integer.parseInt(arr[i])==1) { //오른쪽회전
if(x>=m-1) continue; // 범위 벗어날 경우 무시
dice_t = dice_r;
dice_r = dice_u;
dice_u = dice_l;
dice_l = dice_d;
dice_d = dice_t;
x++;
}else if(Integer.parseInt(arr[i])==2) { //왼쪽회전
if(x<=0) continue;
dice_t = dice_r;
dice_r = dice_d;
dice_d = dice_l;
dice_l = dice_u;
dice_u = dice_t;
x--;
}else if(Integer.parseInt(arr[i])==3) {//위로회전
if(y<=0) continue;
dice_t = dice_u;
dice_u = dice_f;
dice_f = dice_d;
dice_d = dice_b;
dice_b = dice_t;
y--;
}else {//아래로 회전
if(y>=n-1) continue;
dice_t = dice_u;
dice_u = dice_b;
dice_b = dice_d;
dice_d = dice_f;
dice_f = dice_t;
y++;
}
if(map[y][x]==0) {
map[y][x]=dice_d; //주사위밑면 복사
}else {
dice_d=map[y][x]; //지도 밑면 복사
map[y][x]=0; //지도 0으로 초기화
}
System.out.println(dice_u); //주사위 윗면 출력
}
}
}
오답노트 : 문제에서 x가 행이고 y가 열이다.
'백준온라인' 카테고리의 다른 글
[백준온라인] 14501번 퇴사 (0) | 2019.12.04 |
---|---|
[백준온라인] 14500번 테트로미노 (0) | 2019.12.04 |
[백준온라인] 13458번 시험 감독 (0) | 2019.12.04 |
[백준온라인] 3190번 뱀 (0) | 2019.12.04 |
[백준온라인] 12100번 2048 (Easy) (0) | 2019.12.03 |