题意:任意一个点都至少有一个点与其相连接,所有的点可以进行任意排列,总排列数为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. STM32中断定时,控制LED灯

    #include "led.h" void TIM3_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBas ...

  2. [LeetCode&Python] Problem 897. Increasing Order Search Tree

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  3. Unity 3D开发-C#脚本语言的一些基础用法

    Unity 中C#语言的一些基础用法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Lerp -- 线 ...

  4. k8s dockerk个人学习(2)

    部署nginx和应用 部署nginx时我们需要把nginx的配置文件那出来便于以后修改nginx的配置,放置nginx配置文件时我们用k8s的pv的存储方式来存放这些文件 1.安装nfs服务,在kub ...

  5. Android已有的原生Camera框架中加入自己的API的实现方案。

    版权声明:本文为CSDN博主(天才2012)原创文章.未经博主同意不得转载. https://blog.csdn.net/gzzaigcn/article/details/25707389     在 ...

  6. 时间操作(JavaScript版)—页面显示格式:年月日 上午下午 时分秒 星期

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/35222531 <!DOCTYPE ...

  7. JS 中 this 的用法

    this是JavaScript语言中的一个关键字 他是函数运行时,在函数体内部自动生成的一个对象, 只能在函数体内部使用. 在不同function中, this有不同的值. 1. 纯粹的函数调用. f ...

  8. Mysql监控调优

    提升性能 1.允许情况下,调大连接数 2.开启查询缓存(看命中率,用在变化不大的表内) 3.锁(查看是否存在死锁) 4.慢查询(将执行时间过长的语句写入日志内) 5.explain(分析表结构,typ ...

  9. VIPServer:阿里智能地址映射及环境管理系统详解

    http://geek.csdn.net/news/detail/110586 作者: 周遥,阿里技术专家,花名玄胤,毕业于四川大学.六年大型分布式与中间件系统经验,三项国家专利,参加过多次“双十一” ...

  10. linux上通过lighttpd上跑一个C语言的CGI小页面以及所遇到的坑

    Common Gateway Interface如雷贯耳,遗憾的是一直以来都没玩过CGI,今天尝试一把.Tomcat可以是玩CGI的,但得改下配置.为了方便,直接使用一款更轻量级的web服务器ligh ...