카테고리 없음 / / 2020. 3. 3. 13:58

[SWEA_7699 - JAVA] 수지의 수지 맞는 여행

반응형

 

 

풀이

최단 경로였다면 BFS로 풀었겠지만, 가장많이 방문한 횟수를 구해야 하느라 DFS로 구현.
map을 입력받는 즉시 숫자로 치환.
비트 마스킹을 통하여 방문여부 관리.
크게 어렵지 않았던 문제로 비트 마스킹 연습에 좋은 문제.

 

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
 
 
public class Solution {
 
    static int R, C, max; // 행,열
    static int[] dy = { -1100 }; // 상하좌우
    static int[] dx = { 00-11 };
    static int[][] map;
 
    public static boolean isValid(int i, int j) {
        if (i < 0 || j < 0 || i >= R || j >= C) {
            return false;
        }
        return true;
    }
 
    public static void solution(int cnt, int visited, int row, int col) {
 
        visited = visited | (1 << map[row][col]); // 방문 체크
        cnt++// 방문한 개수
 
        max = Math.max(max, cnt);
        if (cnt == 26)
            return;
 
        int nexty, nextx;
        for (int i = 0; i < 4; i++) {
            nexty = row + dy[i];
            nextx = col + dx[i];
            if (isValid(nexty, nextx) && (visited & (1 << map[nexty][nextx])) == 0) { // 범위 검사 && 방문여부
                solution(cnt, visited, nexty, nextx);
            }
        }
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        String str = null;
        int tc = Integer.parseInt(br.readLine());
        for (int test = 1; test <= tc; test++) {
            st = new StringTokenizer(br.readLine());
            R = Integer.parseInt(st.nextToken()); // 행
            C = Integer.parseInt(st.nextToken()); // 열
            map = new int[R][C];
            max = 0;
            for (int i = 0; i < R; i++) {
                str = br.readLine();
                for (int j = 0; j < C; j++) {
                    map[i][j] = str.charAt(j) - 'A';
                }
            }
 
            // ========================== 입력 ==========================
            solution(0000);
            System.out.println("#" + test + " " + max);
        }
 
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWqUzj0arpkDFARG

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유