Home 21772번(가희의 고구마 먹방)
Post
Cancel

21772번(가희의 고구마 먹방)

첫 시도

  • dfs+bfs문제로 예상
  • 아직도 구현이 미숙해 1시간 초과
  • 인터넷에서 답 확인

해결

  • dfs+bfs문제로 예상
  • 아직도 구현이 미숙해 1시간 초과
  • 인터넷에서 답 확인
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
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    static String[][]input;
    static int[]dr= {0,-1,0,1};
    static int[]dc= {-1,0,1,0};
    static int R,C,T;
    public static void main(String[] ars) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine());
        R= Integer.parseInt(st.nextToken());
        C= Integer.parseInt(st.nextToken());
        T= Integer.parseInt(st.nextToken());

        input= new String[R][C];
        Node gahi = new Node(0,0);
        for(int i = 0; i<R; i++){
            String[] temp = br.readLine().split("");
            for(int j = 0; j<C; j++){
                input[i][j] = temp[j];
                if(input[i][j].equals("G")){
                    gahi = new Node(i,j);
                }
            }
        }
        int result =dfs(0, gahi.r, gahi.c);
        System.out.println(result);


    }
    static int dfs(int depth, int r, int c){
        if(depth ==T){
            return 0;
        }
        int count = 0;
        String temp = "";
        int goguma = 0;
        for(int i = 0; i<4; i++){
            int nr = r+dr[i];
            int nc = c+dc[i];
            count = 0;
            if(nr < 0 || nr >=R|| nc < 0 || nc >=C||input[nr][nc].equals("#")){
                continue;
            }
            if(input[nr][nc].equals("S")){
                count++;
            }

            temp =input[nr][nc];
            input[nr][nc] = ".";
            goguma = Math.max(goguma,dfs(depth+1,nr,nc)+count);

            input[nr][nc] = temp;

        }
        return goguma;
    }

}
class Node{
    int r;
    int c;

    Node(int r, int c){
        this.r = r;
        this.c = c;
    }
}

참고

  • https://kwoncorin.tistory.com/116
This post is licensed under CC BY 4.0 by the author.