当前位置: 首页 > article >正文

蓝桥杯——车牌(dfs全排列问题)

一.题目

分析题目:要求6个位置,每个位置放一个数,不能连续三位相同,这些为约束条件也就是减枝函数的内容,最常规的dfs排列问题,只要减枝写对没什么问题

代码:

public static int sum = 0;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = "0123456789ABCDEF";
        char[] ch = s.toCharArray();
        char[] res = new char[6];
        dfs(res,ch,0);
        System.out.println(sum);
        scan.close();
    }
    public static void dfs(char[] res,char[] ch ,int index)
    {
        if(index==6)
        {
            sum++;
            return;
        }
        if(index<3) for(int i = 0;i<16;i++)
        {
            res[index] = ch[i];
            if(cheak(res,index))
                dfs(res,ch,index+1);
        }
        else for(int i = 0;i<10;i++)
            {
                res[index] = ch[i];
                if(cheak(res,index))
                    dfs(res,ch,index+1);
            }
    }
    public static boolean cheak(char[] res,int index)//核心
    {
        if(index<=2)
            return true;
        for(int i = 0;i<index-1;i++)
        {
            if(res[i] == res[i+1]&& res[i+1]==res[i+2])
                return false;
        }
        return true;
    }

一开始错误:

1.把题目中后三位只能是0-9忘记了 导致组合情况出错 粗心问题


http://www.kler.cn/a/588640.html

相关文章:

  • 【R语言入门】向量,存储和计算
  • Oracle Linux Server 7.9安装fail2ban
  • 【QA】建造者模式在Qt有哪些应用
  • Axure设计之动态折线图教程(中继器)
  • 微服务架构: SpringCloud实战案例
  • 基于CSV构建轻量级数据库:SQL与Excel操作的双模实践
  • SSL/TLS 1.2过程:Client端如何验证服务端证书?
  • 鸿蒙 @ohos.arkui.inspector (布局回调)
  • X86 RouterOS 7.18 设置笔记七:不使用Upnp的映射方法
  • 5周0基础冲刺蓝桥杯省重点 1
  • Leetcode3110:字符串的分数
  • 【Function】使用托管身份调用Function App触发器,以增强安全性
  • 使用DeepSeek制作可视化图表和流程图
  • MyBatis源码分析のSql执行流程
  • 在线 SQL 转 flask SQLAlchemy 模型
  • 贪心算法和遗传算法优劣对比——c#
  • 笔记:代码随想录算法训练营day46:LeetCode647. 回文子串\516.最长回文子序列
  • 03 介绍ffmpeg 视频解码流程
  • Ubuntu服务器安装JupyterNotebook,以便通过浏览器访问Jupyter
  • 【数据分析】索引与数据筛选(1)