您的位置:首页 > 其它

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: