uva140-暴力枚举
题意:任意一个点都至少有一个点与其相连接,所有的点可以进行任意排列,总排列数为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-暴力枚举的更多相关文章
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- 51nod 1116 K进制下的大数 (暴力枚举)
题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- bzoj 1028 暴力枚举判断
昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...
- POJ-3187 Backward Digit Sums (暴力枚举)
http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...
- hihoCoder #1179 : 永恒游戏 (暴力枚举)
题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- hdu 1172 猜数字(暴力枚举)
题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...
随机推荐
- 利用WebApplicationInitializer配置SpringMVC取代web.xml
对于Spring MVC的DispatcherServlet配置方式,传统的是基于XML方式的,也就是官方说明的XML-based,如下: <servlet> <servlet-na ...
- Ubuntu 18.10 安装PDF阅读器
======================================== 软件开发转移到了 Linux上,使用Ubuntu 18.10作为桌面开发环境 下面介绍 安装PDF阅读器 1.下载 福 ...
- 一键分享到各个SNS插件
使用百度分享:http://share.baidu.com/code/advance#toid 例: HTML: <div class="bdsharebuttonbox" ...
- HBase scan shell操作详解
创建表 create 'test1', 'lf', 'sf' lf: column family of LONG values (binary value) -- sf: column family ...
- Apache和Nginx的Rewrite规则对比
一.Apache的rewrite 1.Rewrite规则简介: Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的(.h ...
- Mac 下 java环境 maven环境配置
java环境配置 下载jdk,按照提示安装 我下的是1.8.0版本,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...
- Refused to display '[url]' in a frame because it set 'X-Frame-Options' to 'Deny'.
X-Frame-Options是一个HTTP标头(header),用来告诉浏览器这个网页是否可以放在iFrame内.例如: X-Frame-Options: DENY X-Frame-Options: ...
- docker 使用教程(2)常用命令
1. 查看docker信息(version.info) # 查看docker版本$docker version # 显示docker系统的信息$docker info 2. 对image的操作(sea ...
- loadrunner 函数解释
一.lr_save_string 使用介绍1.该函数主要是将程序中的常量或变量保存为lr中的参数.格式: //将常量保存为参数lr_save_string("777"," ...
- Open Flash Chart 之线图
天公司要求开发一个曲线图,简单看了一下之前公司的一个系统,发现一个曲线图效果还不错,查了一下叫OpenFlashChart,还是很不错的,很多人用.研究了一下,发现还不错,特地写了个DEMO测试下. ...