-
Notifications
You must be signed in to change notification settings - Fork 0
/
NM4_backTracking.java
37 lines (27 loc) · 994 Bytes
/
NM4_backTracking.java
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
import java.io.*;
import java.util.StringTokenizer;
public class NM4_backTracking {
static int n,m;
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringBuilder sb = new StringBuilder();
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
backTracking(0,1);
bw.close();
}
public static void backTracking(int depth, int startIndex) throws IOException {
if(depth==m){
bw.write(sb.toString());
bw.write('\n');
return;
}
for(int i=startIndex; i<n+1; i++){
sb.append(i).append(' ');
backTracking(depth+1,i);
sb.delete(sb.length()-2,sb.length());
}
}
}