当前位置:网站首页>The Permutation Results by backtracking method (dfs)
The Permutation Results by backtracking method (dfs)
2022-07-19 07:37: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]);
}
}
边栏推荐
- 面试官:你确定 Redis 是单线程的进程吗?
- Several ways of Intranet penetration
- 【异常】异常解决文章格式
- mysql order by 字段为汉字时
- DOM系列之样式属性操作
- 如何来反向论证产品的好坏?
- Wx: applet value transfer
- Reptile exercises (II)
- Style migration -- sanet: pay attention to any style conversion under the network
- Methods of querying Oracle11g logs, database audit, record troubleshooting
猜你喜欢
Movinets series models are good helpers for real-time classified videos on mobile phones
Several ways of Intranet penetration
查询oracle11g日志的办法,数据库审计,记录排查
Will there be errors when different IPs execute the same SQL script
el-input输入框需要支持多输入
流量控制系统pid整定方法仿真
产品经理必不可少的证书!
Appium automation test foundation - operating wechat applet
关系抽取—OneRel
Restore 360 favorites method after the computer is damaged and the system is reinstalled, put 360sefav_ new_ 2021_ 07_ 16. Favdb files are copied to other computers. Files containing the character new
随机推荐
Redis e-commerce spike design
transformers中BertPreTrainedModel使用说明
爬虫练习题(二)
MySQL调优待完善
MySQL学习笔记——存储过程和函数
产品经理必不可少的证书!
shell函数数组作业
SOC FPGA construction project
Favorite address and historical address of 360 browser
vulnhub Monitoring: 1
Digital signal processing experiment II IIR digital filter design and software implementation
Wireless positioning technology experiment I TDOA-FDOA joint positioning
The El input input box needs to support multiple inputs
Why can't you restore your favorites by copying 360bookmarks to your new computer? Because it's encrypted, you can use 360sefav_ Date Favdb and 360default_ ori_ Date Favdb two favorite backup files im
为什么把 360bookmarks拷到新电脑上无法恢复收藏夹,因为 他是加密的 您可以使用360sefav_日期.favdb和360default_ori_日期.favdb两种收藏夹备份文件导入浏览器
DOM系列之样式属性操作
IPv6-基础
PMP证书含金量如何?值得考吗?
Échec de l'enregistrement des intergiciels personnalisés
LabVIEW在同一个面板下描绘模拟波形和数字波形