蓝桥杯 七段码(只需dfs+sort)

本文介绍了一种使用深度优先搜索(DFS)算法遍历图的方法,并通过排序和集合来去除重复组合,最终计算不重复路径的数量。该方法适用于解决图论中涉及节点间路径计数的问题。
#include<bits/stdc++.h>
using namespace std;


vector<vector<int>>v(8);

vector<int>temp,t;
vector<vector<int>>sum;
bool b[8];
void dfs(int a){
	//cout<<a<<endl;
	b[a]=1;
	temp.push_back(a);
	
	int i=0;
	for(;i<v[a].size();++i){
		if(b[v[a][i]]==0){
			dfs(v[a][i]);
			temp.pop_back();
			b[v[a][i]]=0;

		}
	}
	if(i==v[a].size()){
		t=temp;
		/*		for(int j=0;j<t.size();++j)
			cout<<t[j];
		cout<<endl;*/

		sort(t.begin(),t.end());
		sum.push_back(t);
		
		return; 
	}
}
int main()
{
	
	v[1].push_back(6); v[1].push_back(2);
	v[2].push_back(1); v[2].push_back(7); v[2].push_back(3);
	v[3].push_back(7); v[3].push_back(2); v[3].push_back(4);
	v[4].push_back(5); v[4].push_back(3);
	v[5].push_back(4); v[5].push_back(7);v[5].push_back(6); 
	v[6].push_back(1); v[6].push_back(5);v[6].push_back(7); 	
	v[7].push_back(2); v[7].push_back(5);v[7].push_back(6);v[7].push_back(3); 
	
	/*
	dfs(1);
	for(int i=1;i<=7;++i)
		cout<<b[i]<<" ";
	*/	
	for(int i=1;i<=7;++i){
		temp.clear();
		
		dfs(i);
		b[i]=0;
	}

	/*
	for(int i=0;i<sum.size();++i){
		for(int j=0;j<sum[i].size();++j){
			cout<<sum[i][j];
		}
		cout<<endl;
	}*/
	set<vector<int>>se;
	for(int i=0;i<sum.size();++i){
		se.insert(sum[i]);
	}
	cout<<se.size()<<endl;
	
}

用sort进行排序,同一些字符的不同排列将会被确定为从小到大。最后用集合set来排除相同组合的,得到答案。

### C++ 实现七段码 DFS 算法 以下是基于深度优先搜索(DFS)实现的七段码算法示例代码。该算法的目标是找到满足特定条件的所有可能解,通常涉及回溯和剪枝技术来减少不必要的计算。 #### 1. 问题描述 假设我们需要解决一个与七段数码管相关的问题,比如给定一组数字,找出可以通过点亮某些段显示的有效数字组合。这可以转化为一个典型的排列组合问题,并利用DFS进行求解[^4]。 #### 2. 示例代码 以下是一个完整的C++程序,展示如何使用DFS遍历所有可能的状态并记录符合条件的结果: ```cpp #include <iostream> #include <vector> using namespace std; // 定义全局变量用于存储结果 vector<string> results; string path; // 当前路径 int n; // 数字长度 // 七段数码管对应的每一段状态 (A-G),简化为布尔数组表示 const vector<vector<bool>> segments = { {true, true, true, false, true, true, true}, // '0' {false, false, true, false, false, true, false}, // '1' {true, false, true, true, true, false, true}, // '2' {true, false, true, true, false, true, true}, // '3' {false, true, true, true, false, true, false}, // '4' {true, true, false, true, false, true, true}, // '5' {true, true, false, true, true, true, true}, // '6' {true, false, true, false, false, true, false}, // '7' {true, true, true, true, true, true, true}, // '8' {true, true, true, true, false, true, true} // '9' }; // 判断当前字符是否合法 bool isValid(char c) { return ('0' <= c && c <= '9'); } // 深度优先搜索函数 void dfs(int index, const string& digits) { if (index == digits.size()) { results.push_back(path); // 找到一种有效组合 return; } char currentDigit = digits[index]; if (!isValid(currentDigit)) return; // 如果不是有效的数字,则跳过 int digitValue = currentDigit - '0'; for (int i = 0; i < segments[digitValue].size(); ++i) { if (segments[digitValue][i]) { // 只有当某一位亮起时才继续探索 path += to_string(i); dfs(index + 1, digits); // 继续处理下一个位置 path.pop_back(); // 回溯 } } } int main() { string inputDigits; cout << "请输入要转换的数字字符串:" << endl; cin >> inputDigits; n = inputDigits.length(); dfs(0, inputDigits); // 开始深搜 // 输出结果 cout << "所有可能的七段码组合如下:" << endl; for (auto res : results) { cout << res << endl; } return 0; } ``` #### 3. 关键点解析 - **输入验证**:`isValid()` 函数确保只处理有效的数字字符 `['0', '9']`。 - **递归终止条件**:当索引达到输入字符串的末尾时,保存当前路径作为解决方案的一部分。 - **剪枝策略**:仅在对应位数上允许的情况下深入递归,避免无意义的操作。 #### 4. 运行逻辑说明 上述代码接受用户输入的一串数字,然后尝试枚举每一个数字所代表的所有可能的七段码状态集合。最终输出所有的可行方案列表。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值