当前位置:网站首页>The Permutation Results by backtracking method (dfs)
The Permutation Results by backtracking method (dfs)
2022-07-20 11:57:00 【SUNNY_ CHANGQI】
The description of the problem
Given a vector, you will find all of its permutations results and print them.
The codes 1
#include <bits/stdc++.h>
using namespace std;
void permutation(vector<int> &v, vector<int> &temp, vector<int> &visited);
vector<vector<int>> permutation_results;
int main()
{
vector<int> visited(3, 0);
vector<int> v;
for (int i = 0; i < 3; i++)
{
v.push_back(i);
}
vector<int> temp;
permutation(v, temp, visited);
// cout the permutation_results
for (int i = 0; i < permutation_results.size(); i++)
{
for (int j = 0; j < permutation_results[i].size(); j++)
{
cout << permutation_results[i][j] << " ";
}
cout << endl;
}
return 0;
}
// define a permutation of a vector
void permutation(vector<int> &v, vector<int> &temp, vector<int> &visited)
{
if (temp.size() == v.size()) {
permutation_results.push_back(temp);
return;
}
for (auto ele : v) {
if (visited[ele] == 0) {
visited[ele] = 1;
temp.push_back(ele);
permutation(v, temp, visited);
temp.pop_back();
visited[ele] = 0;
}
}
}
$ ./test
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0
The codes for second method (using swap)
#include <bits/stdc++.h>
using namespace std;
void permutation(vector<int> &v, vector<int> &temp, int cur);
vector<vector<int>> permutation_results;
int main()
{
vector<int> visited(3, 0);
vector<int> v;
for (int i = 0; i < 3; i++)
{
v.push_back(i);
}
vector<int> temp;
permutation(v, temp, 0);
// cout the permutation_results
for (int i = 0; i < permutation_results.size(); i++)
{
for (int j = 0; j < permutation_results[i].size(); j++)
{
cout << permutation_results[i][j] << " ";
}
cout << endl;
}
return 0;
}
// define a permutation of a vector
void permutation(vector<int> &v, vector<int> &temp, int cur)
{
if (cur == v.size())
{
permutation_results.push_back(temp);
return;
}
for (int i = cur; i < v.size(); i++)
{
swap(v[i], v[cur]);
temp.push_back(v[cur]);
permutation(v, temp, cur + 1);
temp.pop_back();
swap(v[i], v[cur]);
}
}
边栏推荐
猜你喜欢
关系抽取—OneRel
【微信小程序】断点调试一
爬虫练习题(二)
为什么把 360bookmarks拷到新电脑上无法恢复收藏夹,因为 他是加密的 您可以使用360sefav_日期.favdb和360default_ori_日期.favdb两种收藏夹备份文件导入浏览器
HCIA-R&S自用笔记(12)路由基础、直连路由与静态路由
Digital signal processing experiment I system response and system stability
结合GUI和simulink的三相电路谐波的检测与建模
SOC FPGA construction project
OSError: exception: access violation writing 0x0000000000000000
MySQL learning notes - View
随机推荐
Appium自动化测试基础 — 操作微信小程序
结合GUI和simulink的三相电路谐波的检测与建模
ES6新增(一)let与常量
Instructions for bertpretrainedmodel in transformers
matlab 有限元计算
DOM系列之DOM事件
Game 302 of leetcode
16.10. Number of survivors
贷款创业遇疫情,年近40的邓子然如何逆风翻盘?
面试官:你确定 Redis 是单线程的进程吗?
vulnhub Monitoring: 1
LabVIEW depicts analog waveform and digital waveform under the same panel
爬虫练习题(二)
360浏览器的收藏夹地址和历史地址
Will there be errors when different IPs execute the same SQL script
五相永磁电机PWM控制系统研究
【NightCafe AI】一分钟创建你想要的NFT数字艺术藏品
每周推荐短视频:对云计算的弹性算力提出了更高要求
为什么把 360bookmarks拷到新电脑上无法恢复收藏夹,因为 他是加密的 您可以使用360sefav_日期.favdb和360default_ori_日期.favdb两种收藏夹备份文件导入浏览器
20220710 leetcode周赛:移动片段得到字符串