[Java/자바 백준 23288] 주사위굴리기2

[Java/자바 백준 23288] 주사위굴리기2

반응형

풀이

삼성 가서 털렸던 문제....

계속 풀고 싶어서 백준에 떠서 풀었는데, 디버깅도 한번도 안하고 바로 맞아버린 비운의 문제..

코테때 로직 그대로 풀었는데....

아무튼 풀이 시작하겠습니다 !

저는 우선 방향을 동남서북으로 맞춰줘서 90도 시계일땐 +1

반시계 90도 일땐 -1 씩으로 dx, dy를 설정했습니다.

처음 동쪽 방향이니 dir=0 으로 설정

isAvailable에서 nx,ny값이 가능한지 보고, 범위 밖이면 dir을 변경해주었습니다.

move 에서 nx,ny로 이동하고

dice_chage에서 주사위를 굴렸습니다.

주사위는

아래 동 서 남 북 위 으로해서

6 3 4 5 2 1 로 초기 설정하고 ,

동서남북에 따라서 주사위가 값들이 어떻게 변경하는지 하나하나 바꿔주고,

dir_change에서

주사위 아랫면 숫자와 map[x][y]값에 따라 주사위가 어느 쪽으로 굴려질지 판단했습니다.

다음 현재위치에서 같은 값에 따라 map[x][y]*갯수 를 total에 더해주고

최종적인 total값을 출력해주었습니다.

import java.io.*; import java.util.*; public class Main { static int N,M,K,dir,cur_x,cur_y,total, point; static int[][] map; static boolean[][] check; static int[] dx = {0,1,0,-1}; static int[] dy = {1,0,-1,0}; static int[] dice = {6,3,4,5,2,1}; static Queue queue = new LinkedList(); 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()); K = Integer.parseInt(st.nextToken()); map = new int[N][M]; total = 0; dir = 0; for(int i=0 ; inx && dir==3) { dir=1; } else if(nx>=N && dir==1) { dir =3; } else if(0>ny && dir==2) { dir=0; } else if(ny>=M && dir==0) { dir = 2; } } private static void move() { // TODO Auto-generated method stub cur_x = cur_x + dx[dir]; cur_y = cur_y + dy[dir]; dice_change(); dir_change(); } private static void dir_change() { // TODO Auto-generated method stub if(dice[0]>map[cur_x][cur_y]) { dir += 1; } else if(dice[0]=4) { dir -=4; } else if(dir<0) { dir +=4; } } private static void dice_change() { // TODO Auto-generated method stub int[] tmp =new int[6]; for(int i=0 ; i<6 ; i++) { tmp[i] = dice[i]; } if(dir == 0) { dice[0]=tmp[1]; dice[1]=tmp[5]; dice[2]=tmp[0]; dice[3]=tmp[3]; dice[4]=tmp[4]; dice[5]=tmp[2]; } else if(dir == 1) { dice[0]=tmp[3]; dice[1]=tmp[1]; dice[2]=tmp[2]; dice[3]=tmp[5]; dice[4]=tmp[0]; dice[5]=tmp[4]; } else if(dir == 2) { dice[0]=tmp[2]; dice[1]=tmp[0]; dice[2]=tmp[5]; dice[3]=tmp[3]; dice[4]=tmp[4]; dice[5]=tmp[1]; } else if(dir == 3) { dice[0]=tmp[4]; dice[1]=tmp[1]; dice[2]=tmp[2]; dice[3]=tmp[0]; dice[4]=tmp[5]; dice[5]=tmp[3]; } } private static void bfs() { // TODO Auto-generated method stub check = new boolean[N][M]; queue.add(new int[] {cur_x,cur_y}); check[cur_x][cur_y] = true; while(!queue.isEmpty()) { int[] cur = queue.poll(); int x = cur[0]; int y = cur[1]; for(int i =0 ; i<4 ; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(0> nx || nx>= N || 0>ny || ny>=M) continue; if(map[x][y] == map[nx][ny] && check[nx][ny]==false) { point ++; check[nx][ny] = true; queue.add(new int[] {nx,ny}); } } } } }

728x90

반응형

from http://kwangkyun-world.tistory.com/102 by ccl(A) rewrite - 2021-11-16 01:27:57