题意:任意一个点都至少有一个点与其相连接,所有的点可以进行任意排列,总排列数为n!.

一个点带宽定义与它相连的点的最远距离,一个排列的带宽定义为,点中最大的带宽,找出带宽最小的那个排列,有多组,输出字典序最小

#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include<time.h>
#include <stdlib.h>
using namespace std;
char v[8];
char edge[100][100];
int tv = 0;
char res[8];
int maxl = 0x7fffffff;
int com(char r[])
{
int max = -1;
for(int i = 0; i < tv; i++)
{
char c = r[i];
for(int j = 0; j < 100; j++)
{
if(edge[c][j] == '\0')
break;
char cc = edge[c][j];
int k = 0;
for(k = 0; k < tv; k++)
{
if(r[k] == cc)
break;
}
int l = k - i;
l = l < 0 ? l * -1 : l;
max = max < l ? l : max;
}
}
if(max < maxl)
{
maxl = max;
memcpy(res, r, sizeof(char) * tv);
}
return 0;
}
void dfs(int cur, int vis[], char r[])
{
if(cur == tv)
{
com(r);
return;
}
for(int i = 0; i < tv; i++)
{
if(vis[i] == 1)
continue;
vis[i] = 1;
r[cur] = v[i];
cur++;
dfs(cur, vis, r);
cur--;
vis[i] = 0;
} } void sort()
{
for(int i = 0; i < tv; i++)
{
for(int j = 1; j < tv - i; j++)
{
if(v[j - 1] > v[j])
{
char t = v[j - 1];
v[j - 1] = v[j];
v[j] = t;
} }
}
} int main()
{
//freopen("d:\\1.txt", "r", stdin);
while (cin)
{
string str;
cin >> str;
if(str == "#")
break;
istringstream is(str);
memset(edge, '\0', sizeof(edge));
memset(v, '\0', sizeof(v));
tv = 0;
maxl = 0x7fffffff;
int index[100];
int mark[100][100];
int mark2[100];
memset(index, 0, sizeof(index));
memset(mark, 0, sizeof(mark));
memset(mark2, 0, sizeof(mark2));
char c;
char cc;
while (is >> c)
{
if(c == ':')
{
continue;
}
if(c == ';')
{
is >> cc;
if(mark2[cc] == 0)
{
v[tv++] = cc;
mark2[cc] = 1;
}
continue;
}
if(tv == 0)
{
cc = c;
mark2[cc] = 1;
v[tv++] = cc;
continue;
}
if(mark[cc][c] != 1)
{
edge[cc][index[cc]++] = c;
mark[cc][c] = 1;
}
if(mark[c][cc] != 1)
{
edge[c][index[c]++] = cc;
mark[c][cc] = 1;
}
if(mark2[c] == 0)
{
mark2[c] = 1;
v[tv++] = c;
}
}
//枚举
int vis[10];
memset(vis, 0, sizeof(vis));
char r[8];
sort();
dfs(0, vis, r); for(int i = 0; i < tv; i++)
cout << res[i] << " ";
cout << "-> " << maxl << endl; }
return 0;
}

  

uva140-暴力枚举的更多相关文章

  1. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  2. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  3. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  4. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  5. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  6. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

  7. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  8. hihoCoder #1179 : 永恒游戏 (暴力枚举)

    题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...

  9. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  10. hdu 1172 猜数字(暴力枚举)

    题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...

随机推荐

  1. C++ 备忘录 (1)

    取模: 1. 转载自:http://ceeji.net/blog/mod-in-real/ 背景 最近在一道 Java 习题中,看到这样的一道题: What is the output when th ...

  2. PR

    3.1音频轨道与播放 单声道音轨: 立体声音轨:立体声音频文件 3.2 音频过渡 需要先把第一段视频的结尾与第二段视频的开头切除,然后在使用恒定功率. 3.3  调音台的简单使用 调节音频: 显示声音 ...

  3. Maven配置dubbo环境简单例子

    环境准备: 1.zookeeper:zookeeper-3.4.6版本 2.maven:apache-maven-3.3.9版本 3.dubbo监控工具:dubbo-admin-2.5.4-SNAPS ...

  4. tarjan强联通分量(模板)

    #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #inc ...

  5. linux内核空间和用户空间详解

    linux驱动程序一般工作在内核空间,但也可以工作在用户空间.下面我们将详细解析,什么是内核空间,什么是用户空间,以及如何判断他们.Linux简化了分段机制,使得虚拟地址与线性地址总是一致,因此,Li ...

  6. 【转】python3中bytes和string之间的互相转换

    问题: 比对算法测试脚本在python2.7上跑的没问题,在python3上报错,将base64转码之后的串打印出来发现,2.7版本和3是不一样的:2.7就是字符串类型的,但是3是bytes类型的,形 ...

  7. js操作链接url

    使用js对当前的URL进行操作,可以使用内置对象window.location: window.location有以下属性: window.location.href:取得当前地址栏中的完整URL,可 ...

  8. StreamSets 多线程 Pipelines

    以下为官方文档: Multithreaded Pipeline Overview A multithreaded pipeline is a pipeline with an origin that ...

  9. MySql初试

    初次使用MySql感觉有点不方便,习惯了使用MS Sql Server带来的便利,话不多说直接进入主题. 第一步.下载MySQL Community Server,下载地址:https://dev.m ...

  10. spring boot 项目文件结构

    启动类建议放在RootPackage的原因: 根据SpringBoot的内置实现,Spring默认从@ComponentScan注解所在的位置开始向后扫描,也就是说,只要我们将组件放在启动类所在的Ro ...