Newer
Older
ResourceDependencyLogic / src / utils / Product.java
@Sakoda2269 Sakoda2269 on 28 Feb 872 bytes 公理による推論を実装中
package utils;

import java.util.ArrayList;
import java.util.List;

public class Product {
	
	public static <T> List<List<T>> product(List<List<T>> lists) {
        List<List<T>> result = new ArrayList<>();
        
        if (lists == null || lists.isEmpty()) {
            return result;
        }

        backtrack(lists, result, 0, new ArrayList<>());
        return result;
    }

    private static <T> void backtrack(
            List<List<T>> lists,
            List<List<T>> result,
            int depth,
            List<T> current) {

        if (depth == lists.size()) {
            result.add(new ArrayList<>(current));
            return;
        }

        for (T element : lists.get(depth)) {
            current.add(element);
            backtrack(lists, result, depth + 1, current);
            current.remove(current.size() - 1);
        }
    }
	
}