• [백준OJ] 14499번 주사위 굴리기

    2021. 4. 23.

    by. Hyeon-Uk

    반응형

    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


     

    -풀이-

    문제에 나온대로 해주었는데, 포인트는 위,아래,동,서,남,북을 바라보는 면의 숫자를 저장해주는 배열을 선언하는 것이다.

    1. 명령에 따른 다음 칸을 확인한 뒤, 범위 밖이면 continue해준다.

    2. 1번을 통과했다면, 다음칸이 0인지 아닌지에 따른 로직을 실행시켜준 뒤, 해당하는 면에 저장해주고, 2차원배열도 업데이트 해준다.

    3.

    동쪽으로 가면 윗면->동쪽, 동쪽->아랫면, 아랫면->서쪽, 서쪽->윗면,

    서쪽으로 가면 윗면->서쪽, 서쪽->아랫면, 아랫면->동쪽, 동쪽->윗면,

    북쪽으로 가면 윗면->북쪽, 북쪽->아랫면, 아랫면->남쪽, 남쪽->윗면,

    남쪽으로 가면 윗면->남쪽, 남쪽->아랫면, 아랫면->북쪽, 북쪽->윗면

    으로 바꿔준 뒤, 윗면을 출력후 주사위의 현재 위치를 업데이트해준다.

     

    -시간복잡도-

    입력 = O(N*M)

    명령을 수행하는 로직에서 내부로직은 모두 상수시간에 해결 가능하므로 O(K)

    N*M<=400, K<=1000 이므로 시간복잡도는 O(K) 가 된다.

     

    -코드-

     

    #include<iostream>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    int n, m, k;
    pair<int, int> dice;
    int mv[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
    int maze[20][20] = { 0 };
    int dice_face[6] = { 0,0,0,0,0,0 };//위,아래,동,서,남,북
    
    bool isIn(int x, int y) {
    	return x >= 0 && x < n && y >= 0 && y <m;
    }
    
    
    int main(){
    	cin >> n >> m >> dice.first >> dice.second >> k;
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < m; j++) {
    			cin >> maze[i][j];
    		}
    	}
    	for (int i = 0; i < k; i++) {
    		int op;
    		cin >> op;
    		int nx = dice.first + mv[op - 1][0];
    		int ny = dice.second + mv[op - 1][1];
    
    		if (!isIn(nx, ny)) {
    			continue;
    		}
    		//동쪽
    		if (op == 1) {
    			if (maze[nx][ny] == 0) {
    				maze[nx][ny] = dice_face[2];
    			}
    			else {
    				dice_face[2] = maze[nx][ny];
    				maze[nx][ny] = 0;
    			}
    			int temp = dice_face[0];
    			dice_face[0] = dice_face[3];
    			dice_face[3] = dice_face[1];
    			dice_face[1] = dice_face[2];
    			dice_face[2] = temp;
    		}
    		//서쪽
    		else if (op == 2) {
    			if (maze[nx][ny] == 0) {
    				maze[nx][ny] = dice_face[3];
    			}
    			else {
    				dice_face[3] = maze[nx][ny];
    				maze[nx][ny] = 0;
    			}
    			int temp = dice_face[0];
    			dice_face[0] = dice_face[2];
    			dice_face[2] = dice_face[1];
    			dice_face[1] = dice_face[3];
    			dice_face[3] = temp;
    		}
    		//북쪽
    		else if (op == 3) {
    			if (maze[nx][ny] == 0) {
    				maze[nx][ny] = dice_face[5];
    			}
    			else {
    				dice_face[5] = maze[nx][ny];
    				maze[nx][ny] = 0;
    			}
    			int temp = dice_face[0];
    			dice_face[0] = dice_face[4];
    			dice_face[4] = dice_face[1];
    			dice_face[1] = dice_face[5];
    			dice_face[5] = temp;
    		}
    		//남쪽
    		else {
    			if (maze[nx][ny] == 0) {
    				maze[nx][ny] = dice_face[4];
    			}
    			else {
    				dice_face[4] = maze[nx][ny];
    				maze[nx][ny] = 0;
    			}
    			int temp = dice_face[0];
    			dice_face[0] = dice_face[5];
    			dice_face[5] = dice_face[1];
    			dice_face[1] = dice_face[4];
    			dice_face[4] = temp;
    		}
    		cout << dice_face[0] << "\n";
    		dice = { nx,ny };
    	}
    }
    
    반응형

    '문제풀이 > 백준oj' 카테고리의 다른 글

    [백준OJ] 14503번 로봇 청소기  (0) 2021.04.24
    [백준OJ] 14502번 연구소  (0) 2021.04.24
    [백준OJ] 3190번 뱀  (0) 2021.04.22
    [백준OJ] 2239번 스도쿠  (0) 2021.04.14
    [백준OJ] 16236번 아기상어  (0) 2021.04.12

    댓글