Publish:

ํƒœ๊ทธ: , , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

img_3.png

img_4.png

์„ค๋ช…

๋ถ€๋“ฑํ˜ธ ๊ธฐํ˜ธ๋“ค์ด ์ฃผ์–ด์ง€๊ณ , ์•ž๋’ค์— 0 ~ 9 ์˜ ์ˆซ์ž๋ฅผ ํ•œ๋ฒˆ์”ฉ๋งŒ ์จ์„œ ๋ชจ๋“  ๋ถ€๋“ฑํ˜ธ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” ๋ฌธ์ œ

ํ’€์ด

  • ์ž…๋ ฅ : ๋ถ€๋“ฑํ˜ธ ๊ฐฏ์ˆ˜๊ฐ€ k ์ด๋ฉด ํ•„์š”ํ•œ ์ˆซ์ž๋Š” k + 1
  • ๋กœ์ง :
    • 0 ~ 9 ๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ dfs ๋Œ๋ฆฐ๋‹ค.
      • ์ด๋•Œ ์„ ํƒํ•œ ์ˆซ์ž๋Š” ์žฌํƒ์ƒ‰ ํ•˜์ง€ ์•Š๋„๋ก visited ๋ฐฐ์—ด๋กœ ์ถ”์ ํ•œ๋‹ค.
      • ํ˜„์žฌ ์„ ํƒํ•œ ์ˆซ์ž๋Š” arr ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
      • dfs ๋ฅผ ํ˜ธ์ถœ ์‹œ depth ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ฆฌ๋ฉด์„œ ํ˜„์žฌ ํƒ์ƒ‰ ๊นŠ์ด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.
    • depth == arr.length ์กฐ๊ฑด ํ™•์ธ :
      • depth ์˜ ํฌ๊ธฐ๊ฐ€ ๋…ธ๋“œ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ์ด๋ฉฐ, arr.length ์™€ ๊ฐ™์œผ๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ธธ์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ๋‹ค์‹œ ์ƒ์œ„ ํ˜ธ์ถœ์‹œ์ ์œผ๋กœ ๋˜๋Œ์•„๊ฐ€์„œ ๋‹ค์Œ ํ•ญ๋ชฉ์„ ํƒ์ƒ‰ ํ•œ๋‹ค. (๋ฐฑํŠธ๋ž˜ํ‚น)
      • ์ตœ๋Œ€ ๊ธธ์ด ๋„๋‹ฌ ์‹œ arr ์— ๋‹ด์•„ ๋‘์—ˆ๋˜ ์ˆซ์ž๊ฐ€ ๋ถ€๋“ฑํ˜ธ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. (isValid())
        • ์กฐ๊ฑด ๋งŒ์กฑ ์‹œ result ์— ๋‹ด์•„๋‘”๋‹ค.
  • ์ถœ๋ ฅ : result ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค.
    1
    
    result = [021, 031, 032, 041, 042, 043, 051, 052, 053, 054, 061, 062, 063, 064, 065, 071, 072, 073, 074, 075, 076, 081, 082, 083, 084, 085, 086, 087, 091, 092, 093, 094, 095, 096, 097, 098, 120, 130, 132, 140, 142, 143, 150, 152, 153, 154, 160, 162, 163, 164, 165, 170, 172, 173, 174, 175, 176, 180, 182, 183, 184, 185, 186, 187, 190, 192, 193, 194, 195, 196, 197, 198, 230, 231, 240, 241, 243, 250, 251, 253, 254, 260, 261, 263, 264, 265, 270, 271, 273, 274, 275, 276, 280, 281, 283, 284, 285, 286, 287, 290, 291, 293, 294, 295, 296, 297, 298, 340, 341, 342, 350, 351, 352, 354, 360, 361, 362, 364, 365, 370, 371, 372, 374, 375, 376, 380, 381, 382, 384, 385, 386, 387, 390, 391, 392, 394, 395, 396, 397, 398, 450, 451, 452, 453, 460, 461, 462, 463, 465, 470, 471, 472, 473, 475, 476, 480, 481, 482, 483, 485, 486, 487, 490, 491, 492, 493, 495, 496, 497, 498, 560, 561, 562, 563, 564, 570, 571, 572, 573, 574, 576, 580, 581, 582, 583, 584, 586, 587, 590, 591, 592, 593, 594, 596, 597, 598, 670, 671, 672, 673, 674, 675, 680, 681, 682, 683, 684, 685, 687, 690, 691, 692, 693, 694, 695, 697, 698, 780, 781, 782, 783, 784, 785, 786, 790, 791, 792, 793, 794, 795, 796, 798, 890, 891, 892, 893, 894, 895, 896, 897]
    
  • ๋งจ ์•ž(์ตœ์†Œ๊ฐ’) ๊ณผ ๋งจ ๋’ค(์ตœ๋Œ€๊ฐ’) ์„ ์ถœ๋ ฅํ•œ๋‹ค.
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
public class Main {
  static int[] arr;
  static boolean[] visited = new boolean[10];
  static String[] sign;
  static List<String> result = new ArrayList<>();

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    sign = br.readLine().split(" ");
    arr = new int[k + 1];

    dfs(0);
    System.out.println(result.get(result.size() - 1));
    System.out.println(result.get(0));
  }

  private static void dfs(int depth) {
    // ๊ธธ์ด ๋„๋‹ฌ ์‹œ ๋ฆฌํ„ด
    if (depth == arr.length) {
      if (isValid()) {   // arr ์˜ ํ•ญ๋ชฉ์ด ๋ถ€๋“ฑํ˜ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
        StringBuilder sb = new StringBuilder();
        for (int num : arr) {
          sb.append(num);
        }
        result.add(sb.toString());
      }
      return;
    }
    
    // 0 ~ 9 ๊นŒ์ง€ ํƒ์ƒ‰ํ•œ๋‹ค
    for (int i = 0; i <= 9; i++) {
      if (!visited[i]) {
        visited[i] = true;   // ์„ ํƒ ์ˆซ์ž ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
        arr[depth] = i;     // ์„ ํƒ ์ˆซ์ž ์ €์žฅ
        dfs(depth + 1);     // ์กฐ๊ฑด ๋งŒ์กฑ ์‹œ ๋‹ค์‹œ ์—ฌ๊ธฐ๋กœ ๋Œ์•„์˜จ๋‹ค
        visited[i] = false;    // ๋ฐฑํŠธ๋ž˜ํ‚น : ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋‹ค์‹œ ๋ฐฉ๋ฌธ ํ•  ์ˆ˜ ์žˆ๋„๋ก false ๋กœ ๋งŒ๋“ ๋‹ค
      }
    }
  }

  // ๋ถ€๋“ฑํ˜ธ ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ œ์™ธ์‹œํ‚จ๋‹ค
  private static boolean isValid() {
    for (int i = 0; i < sign.length; i++) {
      if (sign[i].equals("<")) {
        if (arr[i] >= arr[i + 1]) {
          return false;
        }
      } else if (sign[i].equals(">")) {
        if (arr[i] <= arr[i + 1]) {
          return false;
        }
      }
    }
    return true;
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ