{"id":82218,"date":"2026-07-25T07:18:39","date_gmt":"2026-07-24T23:18:39","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/82218.html"},"modified":"2026-07-25T07:18:39","modified_gmt":"2026-07-24T23:18:39","slug":"java-backtracking-algorithm","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/82218.html","title":{"rendered":"java: Backtracking Algorithm"},"content":{"rendered":"<p>\u9879\u76ee\u7ed3\u6784&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"615\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724231837-6a63f2cd8fc06.png\" width=\"495\" \/><\/p>\n<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:25<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : BraceletBackTracker.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.core;<\/p>\n<p>import Backtracking.dto.BeadItem;<br \/>\nimport Backtracking.rule.IBaseRule;<\/p>\n<p>import java.util.ArrayList;<br \/>\nimport java.util.List;<\/p>\n<p>public class BraceletBackTracker {<\/p>\n<p>    private final List&lt;BeadItem&gt; beadPool;<br \/>\n    private final IBaseRule&lt;BeadItem&gt; rule;<br \/>\n    private final List&lt;List&lt;BeadItem&gt;&gt; solutions &#061; new ArrayList&lt;&gt;();<\/p>\n<p>    public BraceletBackTracker(List&lt;BeadItem&gt; beadPool, IBaseRule&lt;BeadItem&gt; rule) {<br \/>\n        this.beadPool &#061; beadPool;<br \/>\n        this.rule &#061; rule;<br \/>\n    }<\/p>\n<p>    private void backtrack(List&lt;BeadItem&gt; path, int remain, double totalCost, double budget) {<br \/>\n        if (remain &#061;&#061; 0) {<br \/>\n            solutions.add(new ArrayList&lt;&gt;(path));<br \/>\n            return;<br \/>\n        }<br \/>\n        if (totalCost &gt; budget) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        for (BeadItem bead : beadPool) {<br \/>\n            if (!rule.check(bead, path)) {<br \/>\n                continue;<br \/>\n            }<br \/>\n            path.add(bead);<br \/>\n            backtrack(path, remain &#8211; 1, totalCost &#043; bead.getUnitPrice(), budget);<br \/>\n            path.remove(path.size() &#8211; 1);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public List&lt;List&lt;BeadItem&gt;&gt; run(int targetCount, double budget) {<br \/>\n        solutions.clear();<br \/>\n        backtrack(new ArrayList&lt;&gt;(), targetCount, 0.0, budget);<br \/>\n        return solutions;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:26<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : JewelrySceneBackTracker.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.core;<\/p>\n<p>import Backtracking.dto.JewelryItem;<br \/>\nimport Backtracking.rule.IBaseRule;<\/p>\n<p>import java.util.ArrayList;<br \/>\nimport java.util.HashSet;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Set;<\/p>\n<p>public class JewelrySceneBackTracker {<\/p>\n<p>    private final List&lt;JewelryItem&gt; goodsPool;<br \/>\n    private final IBaseRule&lt;JewelryItem&gt; rule;<br \/>\n    private final List&lt;List&lt;JewelryItem&gt;&gt; solutions &#061; new ArrayList&lt;&gt;();<\/p>\n<p>    public JewelrySceneBackTracker(List&lt;JewelryItem&gt; goodsPool, IBaseRule&lt;JewelryItem&gt; rule) {<br \/>\n        this.goodsPool &#061; goodsPool;<br \/>\n        this.rule &#061; rule;<br \/>\n    }<\/p>\n<p>    private void backtrack(int startIdx, List&lt;JewelryItem&gt; selected, double totalPrice, double budget, Set&lt;String&gt; targetCategories) {<br \/>\n        Set&lt;String&gt; selectedCats &#061; new HashSet&lt;&gt;();<br \/>\n        for (JewelryItem item : selected) {<br \/>\n            selectedCats.add(item.getCategory());<br \/>\n        }<\/p>\n<p>        boolean complete &#061; true;<br \/>\n        for (String cat : targetCategories) {<br \/>\n            if (!selectedCats.contains(cat)) {<br \/>\n                complete &#061; false;<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n        if (complete) {<br \/>\n            solutions.add(new ArrayList&lt;&gt;(selected));<br \/>\n            return;<br \/>\n        }<br \/>\n        if (totalPrice &gt; budget) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        for (int i &#061; startIdx; i &lt; goodsPool.size(); i&#043;&#043;) {<br \/>\n            JewelryItem item &#061; goodsPool.get(i);<br \/>\n            if (selectedCats.contains(item.getCategory())) {<br \/>\n                continue;<br \/>\n            }<br \/>\n            if (!rule.check(item, selected)) {<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            selected.add(item);<br \/>\n            backtrack(i &#043; 1, selected, totalPrice &#043; item.getPrice(), budget, targetCategories);<br \/>\n            selected.remove(selected.size() &#8211; 1);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public List&lt;List&lt;JewelryItem&gt;&gt; run(double budget, Set&lt;String&gt; targetCategories) {<br \/>\n        solutions.clear();<br \/>\n        backtrack(0, new ArrayList&lt;&gt;(), 0.0, budget, targetCategories);<br \/>\n        return solutions;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:21<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : BeadItem.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.dto;<\/p>\n<p>\/**<br \/>\n * \u591a\u5b9d\u624b\u4e32\u73e0\u5b50\u5b9e\u4f53<br \/>\n *\/<br \/>\npublic class BeadItem {<br \/>\n    private String beadId;<br \/>\n    private String name;<br \/>\n    private String material;<br \/>\n    private String colorGroup; \/\/ red\/green\/purple\/gold<br \/>\n    private double unitPrice;<br \/>\n    private int stock;<\/p>\n<p>    public String getBeadId() {<br \/>\n        return beadId;<br \/>\n    }<\/p>\n<p>    public void setBeadId(String beadId) {<br \/>\n        this.beadId &#061; beadId;<br \/>\n    }<\/p>\n<p>    public String getName() {<br \/>\n        return name;<br \/>\n    }<\/p>\n<p>    public void setName(String name) {<br \/>\n        this.name &#061; name;<br \/>\n    }<\/p>\n<p>    public String getMaterial() {<br \/>\n        return material;<br \/>\n    }<\/p>\n<p>    public void setMaterial(String material) {<br \/>\n        this.material &#061; material;<br \/>\n    }<\/p>\n<p>    public String getColorGroup() {<br \/>\n        return colorGroup;<br \/>\n    }<\/p>\n<p>    public void setColorGroup(String colorGroup) {<br \/>\n        this.colorGroup &#061; colorGroup;<br \/>\n    }<\/p>\n<p>    public double getUnitPrice() {<br \/>\n        return unitPrice;<br \/>\n    }<\/p>\n<p>    public void setUnitPrice(double unitPrice) {<br \/>\n        this.unitPrice &#061; unitPrice;<br \/>\n    }<\/p>\n<p>    public int getStock() {<br \/>\n        return stock;<br \/>\n    }<\/p>\n<p>    public void setStock(int stock) {<br \/>\n        this.stock &#061; stock;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:21<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : JewelryItem.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.dto;<\/p>\n<p>\/**<br \/>\n * \u6210\u5957\u9996\u9970\u5546\u54c1<br \/>\n *\/<br \/>\npublic class JewelryItem {<br \/>\n    private String skuId;<br \/>\n    private String name;<br \/>\n    private String category; \/\/ necklace \/ earring<br \/>\n    private String material;<br \/>\n    private String color;<br \/>\n    private String style;<br \/>\n    private double price;<br \/>\n    private int stock;<br \/>\n    private boolean hasGem;<\/p>\n<p>    public String getSkuId() {<br \/>\n        return skuId;<br \/>\n    }<\/p>\n<p>    public void setSkuId(String skuId) {<br \/>\n        this.skuId &#061; skuId;<br \/>\n    }<\/p>\n<p>    public String getName() {<br \/>\n        return name;<br \/>\n    }<\/p>\n<p>    public void setName(String name) {<br \/>\n        this.name &#061; name;<br \/>\n    }<\/p>\n<p>    public String getCategory() {<br \/>\n        return category;<br \/>\n    }<\/p>\n<p>    public void setCategory(String category) {<br \/>\n        this.category &#061; category;<br \/>\n    }<\/p>\n<p>    public String getMaterial() {<br \/>\n        return material;<br \/>\n    }<\/p>\n<p>    public void setMaterial(String material) {<br \/>\n        this.material &#061; material;<br \/>\n    }<\/p>\n<p>    public String getColor() {<br \/>\n        return color;<br \/>\n    }<\/p>\n<p>    public void setColor(String color) {<br \/>\n        this.color &#061; color;<br \/>\n    }<\/p>\n<p>    public String getStyle() {<br \/>\n        return style;<br \/>\n    }<\/p>\n<p>    public void setStyle(String style) {<br \/>\n        this.style &#061; style;<br \/>\n    }<\/p>\n<p>    public double getPrice() {<br \/>\n        return price;<br \/>\n    }<\/p>\n<p>    public void setPrice(double price) {<br \/>\n        this.price &#061; price;<br \/>\n    }<\/p>\n<p>    public int getStock() {<br \/>\n        return stock;<br \/>\n    }<\/p>\n<p>    public void setStock(int stock) {<br \/>\n        this.stock &#061; stock;<br \/>\n    }<\/p>\n<p>    public boolean isHasGem() {<br \/>\n        return hasGem;<br \/>\n    }<\/p>\n<p>    public void setHasGem(boolean hasGem) {<br \/>\n        this.hasGem &#061; hasGem;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:22<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : BraceletRule.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.rule;<\/p>\n<p>import Backtracking.dto.BeadItem;<\/p>\n<p>import java.util.HashMap;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Map;<\/p>\n<p>public class BraceletRule implements IBaseRule&lt;BeadItem&gt; {<\/p>\n<p>    private final int maxSingleColor;<\/p>\n<p>    public BraceletRule(int maxSingleColor) {<br \/>\n        this.maxSingleColor &#061; maxSingleColor;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean check(BeadItem item, List&lt;BeadItem&gt; path) {<br \/>\n        \/\/ \u5e93\u5b58\u6821\u9a8c&#xff1a;\u540c\u73e0\u5b50\u9009\u7528\u6b21\u6570\u4e0d\u80fd\u8d85\u8fc7\u5e93\u5b58<br \/>\n        int used &#061; 0;<br \/>\n        for (BeadItem b : path) {<br \/>\n            if (b.getBeadId().equals(item.getBeadId())) {<br \/>\n                used&#043;&#043;;<br \/>\n            }<br \/>\n        }<br \/>\n        if (used &gt;&#061; item.getStock()) {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        \/\/ \u8272\u7cfb\u5747\u8861\u7ea6\u675f<br \/>\n        Map&lt;String, Integer&gt; colorCnt &#061; new HashMap&lt;&gt;();<br \/>\n        for (BeadItem b : path) {<br \/>\n            colorCnt.put(b.getColorGroup(), colorCnt.getOrDefault(b.getColorGroup(), 0) &#043; 1);<br \/>\n        }<br \/>\n        int current &#061; colorCnt.getOrDefault(item.getColorGroup(), 0);<br \/>\n        if (current &gt;&#061; maxSingleColor) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:22<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : IBaseRule.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.rule;<\/p>\n<p>import java.util.List;<\/p>\n<p>\/**<br \/>\n * \u7ea6\u675f\u89c4\u5219\u9876\u5c42\u63a5\u53e3<br \/>\n * &#064;param &lt;T&gt; BeadItem \/ JewelryItem<br \/>\n *\/<br \/>\npublic interface IBaseRule&lt;T&gt; {<br \/>\n    boolean check(T item, List&lt;T&gt; path);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:24<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : SceneConfig.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.rule;<\/p>\n<p>import java.util.*;<\/p>\n<p>public class SceneConfig {<br \/>\n    private Set&lt;String&gt; allowMaterial;<br \/>\n    private boolean mustGem;<\/p>\n<p>    public Set&lt;String&gt; getAllowMaterial() {<br \/>\n        return allowMaterial;<br \/>\n    }<\/p>\n<p>    public void setAllowMaterial(Set&lt;String&gt; allowMaterial) {<br \/>\n        this.allowMaterial &#061; allowMaterial;<br \/>\n    }<\/p>\n<p>    public boolean isMustGem() {<br \/>\n        return mustGem;<br \/>\n    }<\/p>\n<p>    public void setMustGem(boolean mustGem) {<br \/>\n        this.mustGem &#061; mustGem;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:23<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : SceneJewelryRule.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.rule;<\/p>\n<p>import Backtracking.dto.JewelryItem;<\/p>\n<p>import java.util.*;<\/p>\n<p>public class SceneJewelryRule implements IBaseRule&lt;JewelryItem&gt; {<\/p>\n<p>    private final SceneConfig config;<\/p>\n<p>    public SceneJewelryRule(SceneConfig config) {<br \/>\n        this.config &#061; config;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean check(JewelryItem item, List&lt;JewelryItem&gt; path) {<br \/>\n        if (item.getStock() &lt;&#061; 0) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        if (!config.getAllowMaterial().contains(item.getMaterial())) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        if (config.isMustGem() &amp;&amp; !item.isHasGem()) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u573a\u666f\u914d\u7f6e\u4e2d\u5fc3&#xff0c;\u65b0\u589e\u573a\u666f\u4ec5\u5728\u6b64\u6269\u5c55<br \/>\n     *\/<br \/>\n    public static SceneConfig getSceneConfig(String sceneType) {<br \/>\n        Map&lt;String, SceneConfig&gt; sceneMap &#061; new HashMap&lt;&gt;();<\/p>\n<p>        SceneConfig wedding &#061; new SceneConfig();<br \/>\n        wedding.setAllowMaterial(new HashSet&lt;&gt;(List.of(&#034;Au999&#034;, &#034;18K&#034;)));<br \/>\n        wedding.setMustGem(true);<br \/>\n        sceneMap.put(&#034;wedding&#034;, wedding);<\/p>\n<p>        SceneConfig commute &#061; new SceneConfig();<br \/>\n        commute.setAllowMaterial(new HashSet&lt;&gt;(List.of(&#034;Au999&#034;, &#034;S925&#034;, &#034;18K&#034;)));<br \/>\n        commute.setMustGem(false);<br \/>\n        sceneMap.put(&#034;commute&#034;, commute);<\/p>\n<p>        SceneConfig dinner &#061; new SceneConfig();<br \/>\n        dinner.setAllowMaterial(new HashSet&lt;&gt;(List.of(&#034;18K&#034;)));<br \/>\n        dinner.setMustGem(true);<br \/>\n        sceneMap.put(&#034;dinner&#034;, dinner);<\/p>\n<p>        return sceneMap.getOrDefault(sceneType, commute);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:25<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : ScoreUtil.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.common;<\/p>\n<p>import Backtracking.dto.BeadItem;<br \/>\nimport Backtracking.dto.JewelryItem;<\/p>\n<p>import java.util.HashSet;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Set;<\/p>\n<p>public class ScoreUtil {<\/p>\n<p>    \/**<br \/>\n     * \u624b\u4e32\u65b9\u6848\u8bc4\u5206&#xff1a;\u8272\u7cfb\u591a\u6837\u6027\u4f18\u5148<br \/>\n     *\/<br \/>\n    public static double scoreBraceletScheme(List&lt;BeadItem&gt; scheme) {<br \/>\n        Set&lt;String&gt; colorSet &#061; new HashSet&lt;&gt;();<br \/>\n        double totalCost &#061; 0.0;<br \/>\n        for (BeadItem b : scheme) {<br \/>\n            colorSet.add(b.getColorGroup());<br \/>\n            totalCost &#043;&#061; b.getUnitPrice();<br \/>\n        }<br \/>\n        double diversity &#061; colorSet.size();<br \/>\n        return diversity * 10 &#8211; totalCost \/ 200;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u6210\u5957\u9996\u9970\u65b9\u6848\u8bc4\u5206<br \/>\n     *\/<br \/>\n    public static double scoreJewelryScheme(List&lt;JewelryItem&gt; scheme) {<br \/>\n        int gemCnt &#061; 0;<br \/>\n        int stockScore &#061; 0;<br \/>\n        for (JewelryItem item : scheme) {<br \/>\n            if (item.isHasGem()) {<br \/>\n                gemCnt&#043;&#043;;<br \/>\n            }<br \/>\n            stockScore &#043;&#061; Math.min(item.getStock(), 5);<br \/>\n        }<br \/>\n        return gemCnt * 5 &#043; stockScore;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:27<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : BraceletMatchService.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.service;<\/p>\n<p>import Backtracking.common.ScoreUtil;<br \/>\nimport Backtracking.core.BraceletBackTracker;<br \/>\nimport Backtracking.dto.BeadItem;<br \/>\nimport Backtracking.rule.BraceletRule;<br \/>\nimport Backtracking.rule.IBaseRule;<\/p>\n<p>import java.util.ArrayList;<br \/>\nimport java.util.Comparator;<br \/>\nimport java.util.List;<\/p>\n<p>public class BraceletMatchService {<\/p>\n<p>    private final List&lt;BeadItem&gt; beadPool;<\/p>\n<p>    public BraceletMatchService(List&lt;BeadItem&gt; beadPool) {<br \/>\n        this.beadPool &#061; beadPool;<br \/>\n    }<\/p>\n<p>    public List&lt;List&lt;BeadItem&gt;&gt; match(int targetCount, double budget, int maxColorLimit, int topN) {<br \/>\n        IBaseRule&lt;BeadItem&gt; rule &#061; new BraceletRule(maxColorLimit);<br \/>\n        BraceletBackTracker tracker &#061; new BraceletBackTracker(beadPool, rule);<br \/>\n        List&lt;List&lt;BeadItem&gt;&gt; schemes &#061; tracker.run(targetCount, budget);<\/p>\n<p>        \/\/ \u6309\u8bc4\u5206\u964d\u5e8f<br \/>\n        schemes.sort((a, b) -&gt; Double.compare(ScoreUtil.scoreBraceletScheme(b), ScoreUtil.scoreBraceletScheme(a)));<\/p>\n<p>        if (schemes.size() &gt; topN) {<br \/>\n            schemes &#061; schemes.subList(0, topN);<br \/>\n        }<br \/>\n        return new ArrayList&lt;&gt;(schemes);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:28<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : JewelrySceneMatchService.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Backtracking.service;<\/p>\n<p>import Backtracking.common.ScoreUtil;<br \/>\nimport Backtracking.core.JewelrySceneBackTracker;<br \/>\nimport Backtracking.dto.JewelryItem;<br \/>\nimport Backtracking.rule.IBaseRule;<br \/>\nimport Backtracking.rule.SceneJewelryRule;<br \/>\nimport Backtracking.rule.SceneConfig;<\/p>\n<p>import java.util.ArrayList;<br \/>\nimport java.util.Comparator;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Set;<\/p>\n<p>public class JewelrySceneMatchService {<\/p>\n<p>    private final List&lt;JewelryItem&gt; goodsPool;<\/p>\n<p>    public JewelrySceneMatchService(List&lt;JewelryItem&gt; goodsPool) {<br \/>\n        this.goodsPool &#061; goodsPool;<br \/>\n    }<\/p>\n<p>    public List&lt;List&lt;JewelryItem&gt;&gt; matchByScene(String scene, double budget, Set&lt;String&gt; targetCategories, int topN) {<br \/>\n        SceneConfig cfg &#061; SceneJewelryRule.getSceneConfig(scene);<br \/>\n        IBaseRule&lt;JewelryItem&gt; rule &#061; new SceneJewelryRule(cfg);<br \/>\n        JewelrySceneBackTracker tracker &#061; new JewelrySceneBackTracker(goodsPool, rule);<br \/>\n        List&lt;List&lt;JewelryItem&gt;&gt; schemes &#061; tracker.run(budget, targetCategories);<\/p>\n<p>        schemes.sort((a, b) -&gt; Double.compare(ScoreUtil.scoreJewelryScheme(b), ScoreUtil.scoreJewelryScheme(a)));<\/p>\n<p>        if (schemes.size() &gt; topN) {<br \/>\n            schemes &#061; schemes.subList(0, topN);<br \/>\n        }<br \/>\n        return new ArrayList&lt;&gt;(schemes);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8c03\u7528&#xff1a;<\/p>\n<p>\/**<br \/>\n * encoding: utf-8<br \/>\n * \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae<br \/>\n * \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce<br \/>\n * \u63cf\u8ff0&#xff1a;Backtracking Algorithm<br \/>\n * Author    : geovindu,Geovin Du \u6d82\u805a\u6587.<br \/>\n * IDE       : IntelliJ IDEA 2024.3.6 Java 17<br \/>\n * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j<br \/>\n * # OS        : window10<br \/>\n * Datetime  : 2026 &#8211; 2026\/7\/23 &#8211; 22:29<br \/>\n * User      : geovindu<br \/>\n * Product   : IntelliJ IDEA<br \/>\n * Project   : JavaAlgorithms<br \/>\n * File      : BacktrackingBll.java<br \/>\n * explain   : \u5b66\u4e60  \u7c7b<br \/>\n **\/<\/p>\n<p>package Bll;<\/p>\n<p>import Backtracking.dto.BeadItem;<br \/>\nimport Backtracking.dto.JewelryItem;<br \/>\nimport Backtracking.service.BraceletMatchService;<br \/>\nimport Backtracking.service.JewelrySceneMatchService;<\/p>\n<p>import java.util.ArrayList;<br \/>\nimport java.util.HashSet;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Set;<br \/>\nimport java.util.stream.Collectors;<\/p>\n<p>public class BacktrackingBll {<\/p>\n<p>    static void testBraceletMatch() {<br \/>\n        List&lt;BeadItem&gt; beadPool &#061; new ArrayList&lt;&gt;();<br \/>\n        BeadItem b1 &#061; new BeadItem();<br \/>\n        b1.setBeadId(&#034;B01&#034;);<br \/>\n        b1.setName(&#034;\u5357\u7ea2\u5706\u73e0&#034;);<br \/>\n        b1.setMaterial(&#034;\u5357\u7ea2&#034;);<br \/>\n        b1.setColorGroup(&#034;red&#034;);<br \/>\n        b1.setUnitPrice(168);<br \/>\n        b1.setStock(4);<\/p>\n<p>        BeadItem b2 &#061; new BeadItem();<br \/>\n        b2.setBeadId(&#034;B02&#034;);<br \/>\n        b2.setName(&#034;\u548c\u7530\u7389\u5706\u73e0&#034;);<br \/>\n        b2.setMaterial(&#034;\u548c\u7530\u7389&#034;);<br \/>\n        b2.setColorGroup(&#034;green&#034;);<br \/>\n        b2.setUnitPrice(198);<br \/>\n        b2.setStock(5);<\/p>\n<p>        BeadItem b3 &#061; new BeadItem();<br \/>\n        b3.setBeadId(&#034;B03&#034;);<br \/>\n        b3.setName(&#034;\u7d2b\u6c34\u6676&#034;);<br \/>\n        b3.setMaterial(&#034;\u7d2b\u6c34\u6676&#034;);<br \/>\n        b3.setColorGroup(&#034;purple&#034;);<br \/>\n        b3.setUnitPrice(128);<br \/>\n        b3.setStock(4);<\/p>\n<p>        BeadItem b4 &#061; new BeadItem();<br \/>\n        b4.setBeadId(&#034;B04&#034;);<br \/>\n        b4.setName(&#034;\u8db3\u91d1\u9694\u73e0&#034;);<br \/>\n        b4.setMaterial(&#034;\u8db3\u91d1&#034;);<br \/>\n        b4.setColorGroup(&#034;gold&#034;);<br \/>\n        b4.setUnitPrice(320);<br \/>\n        b4.setStock(3);<\/p>\n<p>        beadPool.add(b1);<br \/>\n        beadPool.add(b2);<br \/>\n        beadPool.add(b3);<br \/>\n        beadPool.add(b4);<\/p>\n<p>        BraceletMatchService svc &#061; new BraceletMatchService(beadPool);<br \/>\n        List&lt;List&lt;BeadItem&gt;&gt; result &#061; svc.match(8, 2000, 4, 6);<\/p>\n<p>        System.out.println(&#034;&#061;&#061;&#061;&#061;&#061; \u591a\u5b9d\u624b\u4e32\u642d\u914d\u65b9\u6848 &#061;&#061;&#061;&#061;&#061;&#034;);<br \/>\n        for (int idx &#061; 0; idx &lt; result.size(); idx&#043;&#043;) {<br \/>\n            List&lt;BeadItem&gt; scheme &#061; result.get(idx);<br \/>\n            double total &#061; scheme.stream().mapToDouble(BeadItem::getUnitPrice).sum();<br \/>\n            String names &#061; scheme.stream().map(BeadItem::getName).collect(Collectors.joining(&#034;, &#034;));<br \/>\n            System.out.printf(&#034;\u65b9\u6848%d \u603b\u4ef7:%.2f \u73e0\u5b50:[%s]%n&#034;, idx &#043; 1, total, names);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    static void testJewelrySceneMatch() {<br \/>\n        List&lt;JewelryItem&gt; goodsPool &#061; new ArrayList&lt;&gt;();<\/p>\n<p>        JewelryItem j1 &#061; new JewelryItem();<br \/>\n        j1.setSkuId(&#034;N001&#034;);<br \/>\n        j1.setName(&#034;\u788e\u94bb\u9879\u94fe&#034;);<br \/>\n        j1.setCategory(&#034;necklace&#034;);<br \/>\n        j1.setMaterial(&#034;18K&#034;);<br \/>\n        j1.setColor(&#034;white&#034;);<br \/>\n        j1.setStyle(&#034;luxury&#034;);<br \/>\n        j1.setPrice(3299);<br \/>\n        j1.setStock(12);<br \/>\n        j1.setHasGem(true);<\/p>\n<p>        JewelryItem j2 &#061; new JewelryItem();<br \/>\n        j2.setSkuId(&#034;N003&#034;);<br \/>\n        j2.setName(&#034;\u7d20\u91d1\u9879\u94fe&#034;);<br \/>\n        j2.setCategory(&#034;necklace&#034;);<br \/>\n        j2.setMaterial(&#034;Au999&#034;);<br \/>\n        j2.setColor(&#034;yellow&#034;);<br \/>\n        j2.setStyle(&#034;minimalist&#034;);<br \/>\n        j2.setPrice(2199);<br \/>\n        j2.setStock(9);<br \/>\n        j2.setHasGem(false);<\/p>\n<p>        JewelryItem j3 &#061; new JewelryItem();<br \/>\n        j3.setSkuId(&#034;E001&#034;);<br \/>\n        j3.setName(&#034;\u767d\u94bb\u8033\u9970&#034;);<br \/>\n        j3.setCategory(&#034;earring&#034;);<br \/>\n        j3.setMaterial(&#034;18K&#034;);<br \/>\n        j3.setColor(&#034;white&#034;);<br \/>\n        j3.setStyle(&#034;luxury&#034;);<br \/>\n        j3.setPrice(2199);<br \/>\n        j3.setStock(15);<br \/>\n        j3.setHasGem(true);<\/p>\n<p>        JewelryItem j4 &#061; new JewelryItem();<br \/>\n        j4.setSkuId(&#034;E003&#034;);<br \/>\n        j4.setName(&#034;\u7d20\u91d1\u8033\u9970&#034;);<br \/>\n        j4.setCategory(&#034;earring&#034;);<br \/>\n        j4.setMaterial(&#034;Au999&#034;);<br \/>\n        j4.setColor(&#034;yellow&#034;);<br \/>\n        j4.setStyle(&#034;minimalist&#034;);<br \/>\n        j4.setPrice(1399);<br \/>\n        j4.setStock(11);<br \/>\n        j4.setHasGem(false);<\/p>\n<p>        goodsPool.add(j1);<br \/>\n        goodsPool.add(j2);<br \/>\n        goodsPool.add(j3);<br \/>\n        goodsPool.add(j4);<\/p>\n<p>        JewelrySceneMatchService svc &#061; new JewelrySceneMatchService(goodsPool);<br \/>\n        Set&lt;String&gt; targetCats &#061; new HashSet&lt;&gt;();<br \/>\n        targetCats.add(&#034;necklace&#034;);<br \/>\n        targetCats.add(&#034;earring&#034;);<\/p>\n<p>        System.out.println(&#034;\\\\n&#061;&#061;&#061;&#061;&#061; \u5a5a\u5ac1\u573a\u666f &#061;&#061;&#061;&#061;&#061;&#034;);<br \/>\n        List&lt;List&lt;JewelryItem&gt;&gt; wedding &#061; svc.matchByScene(&#034;wedding&#034;, 8000, targetCats, 8);<br \/>\n        for (List&lt;JewelryItem&gt; set : wedding) {<br \/>\n            String names &#061; set.stream().map(JewelryItem::getName).collect(Collectors.joining(&#034;, &#034;));<br \/>\n            double total &#061; set.stream().mapToDouble(JewelryItem::getPrice).sum();<br \/>\n            System.out.printf(&#034;[%s] \u603b\u4ef7 %.2f%n&#034;, names, total);<br \/>\n        }<\/p>\n<p>        System.out.println(&#034;\\\\n&#061;&#061;&#061;&#061;&#061; \u901a\u52e4\u573a\u666f &#061;&#061;&#061;&#061;&#061;&#034;);<br \/>\n        List&lt;List&lt;JewelryItem&gt;&gt; commute &#061; svc.matchByScene(&#034;commute&#034;, 5000, targetCats, 8);<br \/>\n        for (List&lt;JewelryItem&gt; set : commute) {<br \/>\n            String names &#061; set.stream().map(JewelryItem::getName).collect(Collectors.joining(&#034;, &#034;));<br \/>\n            double total &#061; set.stream().mapToDouble(JewelryItem::getPrice).sum();<br \/>\n            System.out.printf(&#034;[%s] \u603b\u4ef7 %.2f%n&#034;, names, total);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public void Demo()<br \/>\n    {<br \/>\n        testBraceletMatch();<br \/>\n        testJewelrySceneMatch();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"852\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724231837-6a63f2cda7db3.png\" width=\"1395\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u7ed3\u6784&#xff1a;\/*** encoding: utf-8* \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae* \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce* \u63cf\u8ff0&#xff1a;Backtracking Algorithm* Author    : geovindu,Geovin Du \u6d82\u805a\u6587.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c<\/p>\n","protected":false},"author":2,"featured_media":82216,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,149,292,190,427],"topic":[],"class_list":["post-82218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-windows","tag-292","tag-190","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>java: Backtracking Algorithm - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/82218.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"java: Backtracking Algorithm - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u9879\u76ee\u7ed3\u6784&#xff1a;\/*** encoding: utf-8* \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae* \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce* \u63cf\u8ff0&#xff1a;Backtracking Algorithm* Author  : geovindu,Geovin Du \u6d82\u805a\u6587.* IDE    : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/82218.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-24T23:18:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724231837-6a63f2cd8fc06.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82218.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/82218.html\",\"name\":\"java: Backtracking Algorithm - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T23:18:39+00:00\",\"dateModified\":\"2026-07-24T23:18:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/82218.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/82218.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82218.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"java: Backtracking Algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"java: Backtracking Algorithm - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/82218.html","og_locale":"zh_CN","og_type":"article","og_title":"java: Backtracking Algorithm - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u9879\u76ee\u7ed3\u6784&#xff1a;\/*** encoding: utf-8* \u7248\u6743\u6240\u6709 2026 \u00a9\u6d82\u805a\u6587\u6709\u9650\u516c\u53f8 \u00ae* \u8bb8\u53ef\u4fe1\u606f\u67e5\u770b&#xff1a;\u8a00\u8a9e\u6210\u4e86\u9080\u529f\u76e1\u8cac\u7684\u529f\u81e3&#xff0c;\u9084\u9700\u8981\u884c\u7232\u6bcf\u65e5\u4f86\u503c\u73ed\u55ce* \u63cf\u8ff0&#xff1a;Backtracking Algorithm* Author  : geovindu,Geovin Du \u6d82\u805a\u6587.* IDE    : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c","og_url":"https:\/\/www.wsisp.com\/helps\/82218.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T23:18:39+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724231837-6a63f2cd8fc06.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/82218.html","url":"https:\/\/www.wsisp.com\/helps\/82218.html","name":"java: Backtracking Algorithm - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T23:18:39+00:00","dateModified":"2026-07-24T23:18:39+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/82218.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/82218.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/82218.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"java: Backtracking Algorithm"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=82218"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/82216"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=82218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=82218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=82218"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=82218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}