CS/알고리즘_삼성 SW 역량 테스트 기출 문제

삼성 SW 역량 테스트 기출 문제 : 뱀

Jedy_Kim 2021. 10. 13. 16:04
728x90

https://www.acmicpc.net/problem/3190

 

3190번: 뱀

 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

www.acmicpc.net

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.*;
import java.io.*;
// 뱀
// NxN 정사각형 보드
// 맨위 맨 위측 위치(1,1), 처음 오른쪽 방향을 향함
// 뱀은 계속 머리쪽이 늘어남
// 사과 있으면 사과 없어지고 머리 늘려서 꼬리 그대로
// 사과 없으면 머리 늘려서 꼬리가 위치한 칸 비워줌
public class Main {
 
    static int n; // 보드의 크기 (2~100)
    static int k; // 사과의 갯수 (0~100)
    static int l; // 뱀의 방향 변환 횟수 (1~100)
    static int time; // 게임 시간
    static int[][] board;
    
    static List<int[]> snake = new LinkedList<>(); //뱀의 몸통 위치 (x,y)
    
    // 처음 시작은 오른쪽 방향을 보고 있음
    // 0:오른쪽   1:아래쪽   2:왼쪽   3:위
    // D(오른쪽)가 다오면 index++
    // L(왼쪽)이 나오면 index--
    static int index = 0;
    static int[] dx = {010-1}; //세로
    static int[] dy = {10-10}; //가로
    
    static Map<Integer, String> dir; // 뱀의 방향 정보
    
    public static void main(String[] args) throws Exception { 
        BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(System.out));
        BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
 
        n = Integer.parseInt(br.readLine());
        k = Integer.parseInt(br.readLine());
        
        // 1,1이 맨 위 맨 좌측
        board = new int[n+1][n+1];
        
        // 사과 위치 입력
        String str;
        int row; // 행
        int col; // 열
        for(int i=0; i<k; i++) {
            str = br.readLine();
            
            row = Integer.parseInt(str.split(" ")[0]);
            col = Integer.parseInt(str.split(" ")[1]);
            
            board[row][col] = 1;
        }
        
        // 뱀 방향 정보 입력
        dir = new HashMap<>();
        l = Integer.parseInt(br.readLine());
        for(int i=0; i<l; i++) {
            str = br.readLine();
            int timeInfo = Integer.parseInt(str.split(" ")[0]);
            String directionInfo = str.split(" ")[1];
            
            dir.put(timeInfo, directionInfo);
        }
        
        // 뱀 시작 지점 (1,1) (x,y)
        snake.add(new int[]{1,1});
        
        time = 0;
        int nx, ny; // 다음 움직임
        int cx, cy; // 현재 움직임(1,1)
        cx = 1;
        cy = 1;
        // 뱀 움직임 시작
        while(true) {
            time++;
            
            // 다음 움직임(머리 데이터)
            nx = cx + dx[index];
            ny = cy + dy[index];
            
            // 끝나는지 확인
            if(isFinish(nx,ny)) break;
            
            // 사과 있는지 확인
            // 사과 있으면 사과 없어지고 머리 늘려서 꼬리 그대로
            if(board[nx][ny] == 1) {
                board[nx][ny] = 0;
                snake.add(new int[] {nx,ny}); // 머리 정보 추가
            }
            // 사과 없으면 머리 늘려서 꼬리가 위치한 칸 비워줌
            else {
                snake.add(new int[] {nx,ny}); // 머리 정보 추가
                snake.remove(0); // 꼬리 index는 0
            }
             
            cx = nx;
            cy = ny;
            
            // 해당 시간 끝났을 때 다음 방향 정해주기
            if(dir.containsKey(time)) {
                // D(오른쪽)가 다오면 index++
                if(dir.get(time).equals("D")) {
                    index++;
                    if(index == 4)
                        index = 0;
                }
                // L(왼쪽)이 나오면 index--                
                if(dir.get(time).equals("L")) {
                    index--;
                    if(index == -1)
                        index = 3;
                }
                
            }
        }
        
        bw.write(String.valueOf(time));
        br.close();
        bw.flush();
        bw.close();
    }
    
    // 게임이 끝나는지 확인
    static boolean isFinish(int nx, int ny){
        
        // 벽에 부딪히거나
        if(nx<1 || ny<1 || nx>=n+1 || ny>=n+1
            return true;
        
        // 자기 몸통에 닿거나
        for(int i=0; i<snake.size(); i++) {
            if(nx == snake.get(i)[0&& ny == snake.get(i)[1]) 
                return true;
        }
        
        return false;
    }
 
}
cs

 

반응형