uva140
2016-05-07 20:37
204 查看
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19399
/* solution: 在这种没有n皇后一样的可行性约束的问题时可以尝试“递归枚举”+“剪枝” note: 剪枝 date: 2016/5/7 */ #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int INF = 99999999; int graph[10][10], vis[10], node[10], ans[10]; char input[500]; vector<int> tmp; int main() { //freopen("input.txt", "r", stdin); while(~scanf("%s", input) && input[0] != '#') { memset(vis, 0, sizeof(vis)); memset(graph, 0, sizeof(graph)); memset(node, 0, sizeof(node)); memset(ans, 0, sizeof(ans)); tmp.clear(); int len = strlen(input); for(int i = 0; i < len; i++) { if(input[i] >= 'A' && input[i] <= 'Z') { if(!vis[input[i] - 'A']) { tmp.push_back(input[i] - 'A'); vis[input[i] - 'A'] = 1; } } else if(input[i] == ':') { int x = input[i - 1] - 'A'; for(int j = i + 1; input[j] != ';'; j++) { graph[x][input[j] - 'A'] = 1; graph[input[i] - 'A'][x] = 1; } } for(int j = 0; j < tmp.size(); j++) node[j] = tmp[j]; } int num = -1, minBand = INF; int L = tmp.size(); do { for(int i = 0; i < L; i++) for(int j = i + 1; j < L; j++) if(graph[i][j]) if(abs(i - j) > num) num = abs(i - j); if(minBand > num) { //剪枝,若已经发现有两个节点的距离大于或等于k,再怎么扩展 minBand = num; //也不可能比当前更优,所以直接忽略(剪枝),测试下一排列 memcpy(ans, node, L); } } while(next_permutation(node, node + L)); //output the answer for(int i = 0; i < L; i++) printf("%c ", 'A' + node[i]); printf("-> %d\n", minBand); } }
相关文章推荐
- CentOS 6.3中安装Gnome桌面环境
- 06 GUI(图形用户界面)
- jQuery的回调支持
- 中标麒麟在线不重启认存储
- high_memeory 映射---3
- 一台电脑上同启动两个Tomcat的方式,windows/Linux配置。
- PyCharm 设置运行参数
- 一台电脑上同启动两个Tomcat的方式,windows/Linux配置。
- CodeForces 580A Kefa and First Steps
- springMVC知识点学习笔记
- java学习总结(16.05.07) jdk的安装与环境变量的配置
- 算法的力量 ——李开复
- memcachd认识汇总
- java学习之final关键字
- VMwareWorkstation12注册机、您无权输入许可证密钥,请请使用系统管理员账户重试
- HDOJ 3507 斜率优化入门经典题
- LeetCode-103.Binary Tree Zigzag Level Order Traversal
- xcode7 设置启动图片 LaunchImage
- 05 输入输出(IO)
- 区间重叠的判断