Post

백준_17070 (파이프 옮기기)

구현방법

  1. 재귀를 이용하여 DFS를 구현
  2. 파이프의 각 상태에 따라 재귀호출 여부를 동적으로 지정 가로상태? -> 가로 혹은 대각선으로 움직일 수 있는지에 대해 판별 후 dfs 재귀호출 세로상태? -> 세로 혹은 대각선으로 움직일 수 있는지에 대해 판별 후 dfs 재귀호출 대각선상태? -> 가로, 세로 혹은 대각선으로 움직일 수 있는지에 대해 판별 후 dfs 재귀호출

Code

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
package com.company.baekjoon._17070;

import java.io.*;

public class 한규호_17070 {

  static int result = 0;
  static int[][] map;
  static int n;

  static final int vertical = 1; // 수직
  static final int horizontal = 2; // 수평
  static final int diagonal = 3; // 대각선
  static int position;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    n = Integer.parseInt(br.readLine());
    map = new int[n][n];
    for (int i = 0; i < n; i++) {
      String[] line = br.readLine().split(" ");
      for (int j = 0; j < n; j++) {
        map[i][j] = Integer.parseInt(line[j]);
      }
    }
    dfs(0, 0, 0, 1);
    bw.write(Integer.toString(result));
    bw.close();
    br.close();
  }

  public static void dfs(int rearX, int rearY, int frontX, int frontY) {
    if (frontX == n - 1 && frontY == n - 1) {
      result++;
      return;
    }
    position = getPosition(rearX, rearY, frontX, frontY);
    switch (position) {
      case horizontal:
        if (checkHorizontal(frontX, frontY)) {
          dfs(frontX, frontY, frontX, frontY + 1);
        }
        break;
      case vertical:
        if (checkVertical(frontX, frontY)) {
          dfs(frontX, frontY, frontX + 1, frontY);
        }
        break;
      case diagonal:
        if (checkHorizontal(frontX, frontY)) {
          dfs(frontX, frontY, frontX, frontY + 1);
        }
        if (checkVertical(frontX, frontY)) {
          dfs(frontX, frontY, frontX + 1, frontY);
        }
        break;
      default:
        break;
    }
    if (checkDiagonal(frontX, frontY)) {
      dfs(frontX, frontY, frontX + 1, frontY + 1);
    }
  }

  public static int getPosition(int rearX, int rearY, int frontX, int frontY) {
    if (frontX == rearX && frontY - 1 == rearY) {
      return horizontal;
    } else if (frontY == rearY && frontX - 1 == rearX) {
      return vertical;
    } else {
      return diagonal;
    }
  }

  public static boolean checkHorizontal(int frontX, int frontY) {
    return frontY + 1 < n
        && map[frontX][frontY + 1] != 1;
  }

  public static boolean checkVertical(int frontX, int frontY) {
    return frontX + 1 < n
        && map[frontX + 1][frontY] != 1;
  }

  public static boolean checkDiagonal(int frontX, int frontY) {
    return frontY + 1 < n
        && frontX + 1 < n
        && map[frontX][frontY + 1] != 1
        && map[frontX + 1][frontY + 1] != 1
        && map[frontX + 1][frontY] != 1;
  }
}

문제 링크

This post is licensed under CC BY 4.0 by the author.