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
이문제는 주사위 굴리기 2의 하위버전인것같아 쉽게 풀었습니다.
계속 ArrayIndexOutOfRangeException이 나와서 왠지 몰랐는데
보니깐 NxM이기에
x < N 이고 y < M 으로 해야합니다. 계속 y < N으로해서 오류가 나왔습니다.
package Main; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static int N, M; static int x,y,K; static int[][] map; static int[][] dice = { {0,0,0},{0,0,0},{0,0,0},{0,0,0} }; //동쪽,서쪽,북쪽,남쪽 //순서가 좀 이상한데? static int dx[] = {0,0,-1,1}; static int dy[] = {1,-1,0,0}; static int diceposx=0; static int diceposy=0; public static void main(String[] args){ Scanner sc = new Scanner(System.in); N=sc.nextInt(); M=sc.nextInt(); x=sc.nextInt(); y=sc.nextInt(); K=sc.nextInt(); diceposx = x; diceposy = y; map = new int[N][M]; for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { map[i][j] = sc.nextInt(); } } for(int i=0;i<K;i++) { int direction = sc.nextInt(); simulate(direction-1); } } public static void simulate(int direction) { if(diceposx + dx[direction] < 0 || diceposx + dx[direction] >= N || diceposy + dy[direction] < 0 || diceposy + dy[direction] >= M) { return ; } if(diceposx + dx[direction] >= 0 && diceposx + dx[direction] <= N-1 && diceposy + dy[direction] >= 0 && diceposy + dy[direction] <= M-1) { //0: 동쪽 if(direction == 0) { int temp = dice[1][2]; dice[1][2] = dice[1][1]; dice[1][1] = dice[1][0]; dice[1][0] = dice[3][1]; dice[3][1] = temp; } //1: 서쪽 else if(direction == 1) { int temp = dice[1][0]; dice[1][0] = dice[1][1]; dice[1][1] = dice[1][2]; dice[1][2] = dice[3][1]; dice[3][1] = temp; } //2:북쪽 else if(direction == 2) { int temp = dice[0][1]; dice[0][1] = dice[1][1]; dice[1][1] = dice[2][1]; dice[2][1] = dice[3][1]; dice[3][1] = temp; } //3:남쪽 else if(direction == 3) { int temp= dice[3][1]; dice[3][1] = dice[2][1]; dice[2][1] = dice[1][1]; dice[1][1] = dice[0][1]; dice[0][1] = temp; } //주사위 이동 diceposx = diceposx + dx[direction]; diceposy = diceposy + dy[direction]; if(map[diceposx][diceposy] == 0) { map[diceposx][diceposy] = dice[3][1]; }else if(map[diceposx][diceposy] != 0) { dice[3][1] = map[diceposx][diceposy]; map[diceposx][diceposy] = 0; } System.out.println(dice[1][1]); } } }
'알고리즘 > 삼성SW' 카테고리의 다른 글
[삼성 SW 역량 테스트 기출 문제] 아기 상어 - 레벨2, 구현 + BFS + 최단거리 (0) | 2022.07.14 |
---|---|
[삼성 SW 역량 테스트 기출 문제] 스타트와 링크 - 레벨2, 백트래킹 (0) | 2022.07.12 |
[삼성 SW 역량 테스트 기출 문제] 주사위 굴리기 2 - 레벨1 (0) | 2022.07.05 |
[삼성 SW 역량 테스트 기출 문제] 마법사 상어와 비바라기 - 레벨1 (0) | 2022.07.04 |
[삼성 SW 역량 테스트 기출 문제] 마법사 상어와 토네이도 - 레벨1 (0) | 2022.07.02 |