-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExam_01.java
More file actions
52 lines (44 loc) · 1.2 KB
/
Exam_01.java
File metadata and controls
52 lines (44 loc) · 1.2 KB
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
import java.io.FileInputStream;
import java.util.Scanner;
public class Exam_01 {
static int N=5;
static int Answer;
static int [][] input = new int[N][N];
static int visited [] = new int[N];
static int tempAmt;
public static void main(String args[]) throws Exception{
System.setIn(new FileInputStream("src/data/exam01.txt"));
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
//System.out.println(N);
for(int i = 0; i < N ; i++){
visited [i] = 0;
for(int j = 0; j < N; j++){
input[i][j]= sc.nextInt();
}
}
Answer = Integer.MAX_VALUE;
backtrack(0);
System.out.println("# " + Answer);
}
public static void backtrack(int depth){
if(depth==N){
if(tempAmt < Answer) Answer = tempAmt;
return;
}
for(int i=0; i<N ; i++){
if(visited[i]==0){
//System.out.println(depth+":"+i+"="+input[depth][i]+","+tempAmt);
tempAmt = tempAmt + input[depth][i];
if(tempAmt > Answer) {
tempAmt = tempAmt - input[depth][i];
continue;
}
visited[i]=1;
backtrack(depth+1);
tempAmt = tempAmt - input[depth][i];
visited[i]=0;
}
}
}
}