-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProgrammingRow.java
More file actions
32 lines (30 loc) · 974 Bytes
/
Copy pathDynamicProgrammingRow.java
File metadata and controls
32 lines (30 loc) · 974 Bytes
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
import java.util.*;
public class DynamicProgrammingRow {
static int maxSum = Integer.MIN_VALUE;
public static void solve(int[][] a, int row, boolean[] used, int sum) {
int n = a.length;
if (row == n) {
maxSum = Math.max(maxSum, sum);
return;
}
for (int col = 0; col < n; col++) {
if (!used[col]) {
used[col] = true;
solve(a, row + 1, used, sum + a[row][col]);
used[col] = false;
}}}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if (!sc.hasNextInt()) return;
int n = sc.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
boolean[] used = new boolean[n];
solve(a, 0, used, 0);
System.out.println(maxSum);
}
}