77public class Main {
88 static int N ;
99 static int count ;
10+ static int [][] state ;
11+
1012 public static void main (String [] args ) throws IOException {
1113
1214 BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
1315 N = Integer .parseInt (br .readLine ());
14- int [][] state = new int [N ][N ];
16+ state = new int [N ][N ];
1517 int [][] visited = new int [N ][N ];
1618
1719 for (int i = 0 ; i < N ; i ++) {
@@ -20,28 +22,46 @@ public static void main(String[] args) throws IOException {
2022 state [i ][j ] = Integer .parseInt (s [j ]);
2123 }
2224 }
23- DFS (0 ,1 , 0 );
24-
25+ DFS (0 , 1 , 0 );
26+ System . out . println ( count );
2527
2628
2729 }
2830
2931 private static void DFS (int col , int row , int direction ) {
30- if (col == N - 1 && col == N - 1 ) {
32+ if (col == N - 1 && row == N - 1 ) {
3133 count ++;
3234 return ;
3335 }
34- if (direction ==0 ){
35- if (row ==N -1 ){
36- return ;
36+ if (direction == 0 ) {
37+
38+ if (row + 1 < N && state [col ][row + 1 ] == 0 ) {
39+ DFS (col , row + 1 , 0 );
40+ }
41+ if (row + 1 < N && col + 1 < N && state [col + 1 ][row + 1 ] == 0 && state [col ][row + 1 ] == 0 && state [col + 1 ][row ] == 0 ) {
42+ DFS (col + 1 , row + 1 , 2 );
3743 }
3844
3945
40- }
41- else if (direction ==1 ){
46+ } else if (direction == 1 ) {
4247
43- }
44- else if (direction ==2 ){
48+ if (col + 1 < N && state [col + 1 ][row ] == 0 ) {
49+ DFS (col + 1 , row , 1 );
50+ }
51+ if (row + 1 < N && col + 1 < N && state [col + 1 ][row + 1 ] == 0 && state [col ][row + 1 ] == 0 && state [col + 1 ][row ] == 0 ) {
52+ DFS (col + 1 , row + 1 , 2 );
53+ }
54+
55+ } else if (direction == 2 ) {
56+ if (row + 1 < N && state [col ][row + 1 ] == 0 ) {
57+ DFS (col , row + 1 , 0 );
58+ }
59+ if (col + 1 < N && state [col + 1 ][row ] == 0 ) {
60+ DFS (col + 1 , row , 1 );
61+ }
62+ if (row + 1 < N && col + 1 < N && state [col + 1 ][row + 1 ] == 0 && state [col ][row + 1 ] == 0 && state [col + 1 ][row ] == 0 ) {
63+ DFS (col + 1 , row + 1 , 2 );
64+ }
4565
4666 }
4767 }
0 commit comments