File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int N , M ;
7+ static boolean [][] grid;
8+ static long count = 0 ;
9+
10+ public static void main (String [] args ) throws IOException {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
12+ StringTokenizer st = new StringTokenizer (br. readLine());
13+ N = Integer . parseInt(st. nextToken());
14+ M = Integer . parseInt(st. nextToken());
15+ grid = new boolean [N ][M ];
16+
17+ backtrack(0 );
18+ System . out. println(count);
19+ }
20+
21+ static void backtrack (int pos ) {
22+ if (pos == N * M ) {
23+ count++ ;
24+ return ;
25+ }
26+
27+ int row = pos / M ;
28+ int col = pos % M ;
29+
30+ grid[row][col] = false ;
31+ backtrack(pos + 1 );
32+
33+ grid[row][col] = true ;
34+ if (row > 0 && col > 0 && grid[row- 1 ][col] && grid[row][col- 1 ] && grid[row- 1 ][col- 1 ]) {
35+ grid[row][col] = false ;
36+ return ;
37+ }
38+ backtrack(pos + 1 );
39+ grid[row][col] = false ;
40+ }
41+ }
42+ ```
You can’t perform that action at this time.
0 commit comments