1
+ /*
2
+ * Author: Minho Kim (ISKU)
3
+ * Date: July 27, 2019
4
+ * E-mail: minho.kim093@gmail.com
5
+ *
6
+ * https://github.com/ISKU/Algorithm
7
+ * https://www.acmicpc.net/problem/17370
8
+ */
9
+
10
+ import java .io .*;
11
+
12
+ public class Main {
13
+
14
+ private static boolean [][] map ;
15
+ private static int N , answer ;
16
+
17
+ public static void main (String [] args ) throws Exception {
18
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
19
+ N = Integer .parseInt (br .readLine ());
20
+
21
+ map = new boolean [50 ][50 ];
22
+ map [25 ][25 ] = true ;
23
+
24
+ dfs (24 , 25 , 1 , 0 );
25
+ System .out .println (answer );
26
+ }
27
+
28
+ private static void dfs (int y , int x , int dir , int depth ) {
29
+ if (depth == N ) {
30
+ if (map [y ][x ])
31
+ answer ++;
32
+ return ;
33
+ }
34
+ if (map [y ][x ])
35
+ return ;
36
+
37
+ if (dir == 1 ) {
38
+ map [y ][x ] = true ;
39
+ dfs (y - 1 , x + 1 , 3 , depth + 1 );
40
+ dfs (y - 1 , x - 1 , 5 , depth + 1 );
41
+ map [y ][x ] = false ;
42
+ }
43
+ else if (dir == 2 ) {
44
+ map [y ][x ] = true ;
45
+ dfs (y + 1 , x + 1 , 4 , depth + 1 );
46
+ dfs (y + 1 , x - 1 , 6 , depth + 1 );
47
+ map [y ][x ] = false ;
48
+ }
49
+ else if (dir == 3 ) {
50
+ map [y ][x ] = true ;
51
+ dfs (y - 1 , x , 1 , depth + 1 );
52
+ dfs (y + 1 , x + 1 , 4 , depth + 1 );
53
+ map [y ][x ] = false ;
54
+ }
55
+ else if (dir == 4 ) {
56
+ map [y ][x ] = true ;
57
+ dfs (y + 1 , x , 2 , depth + 1 );
58
+ dfs (y - 1 , x + 1 , 3 , depth + 1 );
59
+ map [y ][x ] = false ;
60
+ }
61
+ else if (dir == 5 ) {
62
+ map [y ][x ] = true ;
63
+ dfs (y - 1 , x , 1 , depth + 1 );
64
+ dfs (y + 1 , x - 1 , 6 , depth + 1 );
65
+ map [y ][x ] = false ;
66
+ }
67
+ else {
68
+ map [y ][x ] = true ;
69
+ dfs (y + 1 , x , 2 , depth + 1 );
70
+ dfs (y - 1 , x - 1 , 5 , depth + 1 );
71
+ map [y ][x ] = false ;
72
+ }
73
+ }
74
+ }
0 commit comments