왜 알고리즘은 하면 할수록 어려운 걸까.. 알고리즘 잘 하고 싶다ㅜㅜ

2017 상반기 삼성 SW 오후 문제

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
import java.util.Scanner;
import java.io.FileInputStream;
 
class Solution {
    static int n;
    static int m;
    static int map[][];
    static int cnt = 0;
 
    static int dx[] = { -1010 };
    static int dy[] = { 010-1 };
 
    public static void main(String args[]) throws Exception {
        // Scanner sc = new Scanner(new FileInputStream("sample_input.txt"));
 
        Scanner sc = new Scanner(System.in);
 
        n = sc.nextInt();
        m = sc.nextInt();
        map = new int[n][m];
 
        int curr_x = sc.nextInt();
        int curr_y = sc.nextInt();
        int curr_d = sc.nextInt();
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[i][j] = sc.nextInt();
            }
        }
 
        solve(curr_x, curr_y, curr_d);
 
        System.out.println(cnt);
 
    } // main
 
    static void solve(int curr_x, int curr_y, int curr_d) {
        if (map[curr_x][curr_y] == 1)
            return;
 
        if (map[curr_x][curr_y] == 0) {
            map[curr_x][curr_y] = 2;
            cnt++;
        }
 
        for (int i = 0; i < 4; i++) {
            int next_d = (curr_d + 3) % 4;
            int next_x = curr_x + dx[next_d];
            int next_y = curr_y + dy[next_d];
 
            if (map[next_x][next_y] == 0) {
                solve(next_x, next_y, next_d);
                return;
            } else {
                curr_d = next_d;
            }
        }
 
        if (curr_d == 0) {
            curr_x++;
        } else if (curr_d == 1) {
            curr_y--;
        } else if (curr_d == 2) {
            curr_x--;
        } else if (curr_d == 3) {
            curr_y++;
        }
        solve(curr_x, curr_y, curr_d);
 
    } 
 
}
cs

'공부 > 알고리즘문제' 카테고리의 다른 글

백준 16234 인구 이동  (0) 2019.04.08
백준 1182 [비트 마스크] 부분집합의 합  (0) 2018.07.29
백준 14500 테트로미노  (0) 2017.04.24
백준 7562 나이트의 이동  (0) 2017.04.15
백준 2178 미로 탐색  (0) 2017.04.15