public static void func(int n, int m, int idx, int depth, String str){
if(depth == m){
System.out.println(str);
return;
}
for(int i=1;i<=n;i++){
if(!isPrinted[i]){
isPrinted[i] = true;
if(!str.isEmpty()) func(n,m,i+1, depth+1, str + " " + i);
else func(n,m,i+1, depth+1, "" + i);
isPrinted[i] = false;
}
}
}
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
public static void func(int n, int m, int idx, int depth, String str){
if(depth == m){
System.out.println(str);
return;
}
for(int i=idx;i<=n;i++){
if(!str.isEmpty()) func(n,m,i+1, depth+1, str + " " + i);
else func(n,m,i+1, depth+1, "" + i);
}
}
1 2
1 3
1 4
2 3
2 4
3 4
isPrinted[i]
의 유무import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
public class Main{
static int n;
static int m;
static int favorites[][];
static boolean visited[];
static ArrayList<Integer> list = new ArrayList<>();
static int max = 0;
public static int getFavorites(ArrayList<Integer> list){
int sum = 0;
for(int i=0;i<n;i++){
int personMax = 0;
for(Integer item:list){
personMax = Math.max(personMax,favorites[i][item]);
}
sum+= personMax;
}
return sum;
}
public static void dfs(int depth, int idx){
if(list.size()==3) {
max = Math.max(max, getFavorites(list));
return;
}
for(int i=idx;i<m;i++){
list.add(i);
int idx1 = list.size()-1;
dfs(depth+1, i+1);
list.remove(idx1);
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String str[] = br.readLine().split(" ");
n = Integer.parseInt(str[0]);
m = Integer.parseInt(str[1]);
favorites = new int[n][m];
visited = new boolean[m];
for(int i=0;i<n;i++){
String input[] = br.readLine().split(" ");
for(int j=0;j<m;j++){
favorites[i][j] = Integer.parseInt(input[j]);
}
}
for(int i=0;i<m;i++){
visited = new boolean[m];
list.clear();
dfs(0,i);
}
bw.write(max+"");
bw.flush();
bw.close();
}
}
Four Squares