https://www.acmicpc.net/problem/23288
23288번: 주사위 굴리기 2
크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. 가장 왼
www.acmicpc.net
이 문제는 완전 구현문제 + dfs 가 살짝 섞여있는 문제입니다.
혼자서 처음부터 dfs와 direction 쪽 예제코드만 보고 풀었습니다.
direction = (direction == 3) ? 0 : direction + 1;
direction = (direction == 0) ? 3 : direction - 1;
package Main; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { //세로크기 N, 가로크기 M, (2 <= N,M <= 20) 이동하는 횟수 K (1 <= K <= 1000) static int N,M,K; //처음에는 동쪽 static int direction=0; //동쪽,남쪽,서쪽,북쪽 static int[] dx = {0,1,0,-1}; static int[] dy = {1,0,-1,0}; //지도의 각 칸에 쓰여있는 수는 10 미만의 자연수 static int[][] map; // static int resultscore=0; static int count = 0; static int dicepositionx=0; static int dicepositiony=0; static int[][] dice = {{0,2,0}, {4,1,3}, {0,5,0}, {0,6,0} }; static boolean[][] visited; public static void main(String[] args){ Scanner sc = new Scanner(System.in); N=sc.nextInt(); M=sc.nextInt(); K=sc.nextInt(); 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++) { simulatedice(); } System.out.println(resultscore); } public static void simulatedice() { //주사위 이동방향에 칸이 있는지 확인 if( dicepositionx + dx[direction] >= 0 && dicepositionx + dx[direction] <= N -1 && dicepositiony + dy[direction] >= 0 && dicepositiony + dy[direction] <= M-1) { }else { direction = (direction+2)%4; } 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; }else if(direction==1) { //남쪽 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; }else if(direction==2) { //서쪽 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; }else if(direction==3) { //북쪽 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; } dicepositionx += dx[direction]; dicepositiony += dy[direction]; count = 0; visited = new boolean[N][M]; int standardvalue = map[dicepositionx][dicepositiony]; mapdfs(dicepositionx, dicepositiony, standardvalue); resultscore += count * standardvalue; //3.주사위 이동방향 결정 if(dice[3][1] > map[dicepositionx][dicepositiony]) { direction = (direction == 3) ? 0 : direction + 1; //direction = (direction + 1) % 4; }else if(dice[3][1] < map[dicepositionx][dicepositiony]) { direction = (direction == 0) ? 3 : direction - 1; }else if(dice[3][1] == map[dicepositionx][dicepositiony]){ } } public static void mapdfs(int x, int y, int standardvalue) { if(map[x][y] == standardvalue) { visited[x][y] = true; count+=1; }else { return ; } for(int i=0;i<4;i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(nx >= 0 && nx <N && ny >= 0 && ny <M) { if(!visited[nx][ny] && map[nx][ny] == standardvalue) { mapdfs(nx, ny, standardvalue); } } } } }
23288번: 주사위 굴리기 2
크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. 가장 왼
www.acmicpc.net
'알고리즘 > 삼성SW' 카테고리의 다른 글
[삼성 SW 역량 테스트 기출 문제] 스타트와 링크 - 레벨2, 백트래킹 (0) | 2022.07.12 |
---|---|
[삼성 SW 역량 테스트 기출 문제] 주사위 굴리기 - 레벨2 (0) | 2022.07.06 |
[삼성 SW 역량 테스트 기출 문제] 마법사 상어와 비바라기 - 레벨1 (0) | 2022.07.04 |
[삼성 SW 역량 테스트 기출 문제] 마법사 상어와 토네이도 - 레벨1 (0) | 2022.07.02 |
[삼성 SW 역량 테스트 기출 문제] 미세먼지 안녕! - 레벨1 (0) | 2022.04.26 |