POJ 1270 Following Orders(拓扑排序:输出所有可能)
来源:互联网 发布:win10禁止安装软件设置 编辑:程序博客网 时间:2024/05/04 15:43
题意:给出一串字母和关系,问输出所有可能满足拓扑排序的排列组合
思路:由于要输出所有的组合并且题目保证一定有输出,那么可以用dfs来构造组合。需要满足两个条件
1.当前被选的字母必须有效(即mark[i]==true)且当前被选的字母vis=false(即还没被选)。
2.当我们从前到后依次选择一个字母x放进topo数组的时候,我们要保证在topo数组的当前位置cnt的前面那些位置中不会出现y这种字母。其中y<x,即y被要求出现在x后面。
#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 30#define LL long longint cas=1,T;int G[maxn][maxn];int vis[maxn];int ans[maxn];int n,cnt;int mark[maxn];bool ok(int i,int cnt){for (int j = 0;j<cnt;j++)if (G[i][ans[j]])return false;return true;}void dfs(int cnt){if (cnt==n){for (int i = 0;i<n;i++) printf("%c",ans[i]+'a');printf("\n");}else{for (int i = 0;i<26;i++)if (mark[i] && !vis[i] && ok(i,cnt)){vis[i]=1;ans[cnt]=i;dfs(cnt+1);vis[i]=0;}}}int main(){char str[1000];while (gets(str)){n=0;memset(mark,0,sizeof(mark));memset(G,0,sizeof(G));memset(vis,0,sizeof(vis));for (int i = 0;str[i];i++)if(str[i]!=' '){mark[str[i]-'a']=1;n++;}gets(str);for (int i = 0;str[i];i++)if (str[i]!=' '){int a,b;a=str[i++]-'a';while (str[i]==' ')i++;b=str[i]-'a';G[a][b]=1;}dfs(0);printf("\n");}//freopen("in","r",stdin);//scanf("%d",&T);//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 0;}
0 0
- POJ 1270 Following Orders(拓扑排序:输出所有可能)
- POJ 1270 Following Orders(拓扑排序:输出所有可能)
- POJ 1270 Following Orders 拓扑排序全输出
- POJ 1270 Following Orders (拓扑排序)
- POJ 1270 Following Orders 拓扑排序
- POJ 1270 Following Orders 拓扑排序
- poj 1270 Following Orders(拓扑排序+dfs)
- poj-1270 Following Orders(拓扑排序)
- POJ 1270 Following Orders(拓扑排序)
- Poj 1270 Following Orders【Dfs拓扑排序】
- poj 1270 Following Orders 拓扑
- poj 1270 || uva 124 Following Orders (拓扑排序)
- poj 1270 Following Orders(DFS+拓扑排序)
- poj 1270 Following Orders (简单的拓扑排序)
- POJ:1270 Following Orders (全拓扑排序)
- poj 1270&& uva 124 Following Orders(拓扑排序、DFS)
- UVA 124 & POJ 1270 Following Orders(拓扑排序)
- POJ 270 Following Orders 拓扑排序
- 安卓手机虚拟按键
- POJ 2516 Minimal Cost(最小费用最大流)
- poj3278 BFS基础
- android wear端数据和手机端数据
- Operations-ansible-01
- POJ 1270 Following Orders(拓扑排序:输出所有可能)
- Operations-ansible-02
- 掌握需求过程
- 图像平均及其在降噪方面的应用
- extjs查找表单中的文本框值
- 美化博客的一些实用方法
- 思考
- 《App研发录》读书笔记 (4) 第4章 Android命名规范和编码规范
- MongoDB_类型操作符