uva 140 Bandwidth(全排列+递归)

快睡觉的时候1A的把序列全排列,递归暴力判断就ok啦,我改成对应的整数存了,a数组存的是所有的字符的排列,

b数组存的是所有开始节点的排列,map[i][j]数组存的是i为起点,与j相邻

贴代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<algorithm>
using namespace std;
int map[10][30];
int a[30];
int b[10];
int visit[30];
int Min,s,Max2;
int ma[30];
int cmp(const void *c,const void *d)
{
	return *(int *)c - *(int *)d;
}
void dfs(int x,int y)
{
	int Max1 = 0;
	for(int i=0; i<s; i++)
	{
		if(map[x][a[i]] == 1)
		{
			if(abs(y-i) > Max1)
				Max1 = abs(y-i);
		}
	}
	if(Max1 > Max2)
		Max2 = Max1;
	return ;
}
int main()
{
	char ch;
	int i,j,flag = 1,k=0;
	s = 0;
	Min = INT_MAX;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(visit,0,sizeof(visit));
	memset(map,0,sizeof(map));
	while(scanf("%c",&ch),ch!='#')
	{
		if(ch >= 'A' && ch <= 'Z' && (visit[ch-'A'] == 0))
		{
			visit[ch - 'A'] = 1;
			a[s++] = ch - 'A';
		}
		if(ch >= 'A' && ch <= 'Z' && (flag == 1))
			{
			    i = ch - 'A';
			    b[k++] = i;
			}
		else if(ch >= 'A' && ch <= 'Z' && (flag == 0))
			map[i][ch-'A'] = 1;
		else if(ch == ':')
			flag = 0;
		else if(ch == ';')
		{
			flag = 1;
			continue;
		}
		else if(ch == '\n')
		{
			qsort(a,s,sizeof(a[0]),cmp);
			do
			{
				Max2 = 0;
				for(i=0; i<s; i++)
				{
					for(j=0; j<k; j++)
					{
						if(a[i] == b[j])
							dfs(a[i],i);
					}
				}
				if(Max2 < Min)
					{
						Min = Max2;
						for(int p=0; p<s; p++)
						{
							ma[p] = a[p];
						}
					}
			}while(next_permutation(a,a+s));
			for(i=0; i<s; i++)
				printf("%c ",ma[i]+'A');
				printf("-> ");
			printf("%d\n",Min);
				memset(a,0,sizeof(a));
				memset(b,0,sizeof(b));
				memset(visit,0,sizeof(visit));
				memset(map,0,sizeof(map));
				s = k = 0;
				flag = 1;
				Min = INT_MAX;
		}

	}
	return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 14:28:06

uva 140 Bandwidth(全排列+递归)的相关文章

uva 140 Bandwidth (全排列+暴力枚举)

uva 140 Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it

[2016-02-20][UVA][140][Bandwidth]

UVA - 140 Bandwidth Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth 

UVA - 140 Bandwidth(带宽)(全排列)

题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #i

uva 140 bandwidth (好题) ——yhx

 Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is con

UVa 140 Bandwidth(DFS 回溯 剪枝)

题意  有一个无向图  对于其所有顶点的一个排列 称一顶点与所有与其有边的其它顶点在排列中下标差的最大值为这个顶点的带宽   而所有顶点带宽的最大值为这个排列的带宽   求这个图带宽最小的排列 枚举排列  ans记录当前找到的最小带宽  枚举过程中一旦出现带宽大于ans的也就不用再扩展了  这样枚举完就得到了答案 #include<cstdio> #include<cstring> using namespace std; const int N = 50; int n, ans,

全排列(递归与非递归实现)

全排列问题在公司笔试的时候很常见,这里介绍其递归与非递归实现. 递归算法 1.算法简述 简单地说:就是第一个数分别以后面的数进行交换 E.g:E = (a , b , c),则 prem(E)= a.perm(b,c)+ b.perm(a,c)+ c.perm(a,b) 然后a.perm(b,c)= ab.perm(c)+ ac.perm(b)= abc + acb.依次递归进行. void swap(string &pszStr,int k,int m) { if(k==m) return ;

全排列递归实现(二)

/**  * @param args  */ public static void main(String[] args) { char[] raw = "12345".toCharArray(); perm(raw, 4); // System.out.println("==========================="); // System.out.println(Arrays.toString(raw)); } static void perm(cha

UVa 140 (枚举排列) Bandwidth

题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. 还有就是不明白,为什么19.20行注释哪错了?? 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10; 5 6 int id[256], letter[maxn]; 7 char in[1000];

Uva140 Bandwidth 全排列+生成测试法+剪枝

参考过仰望高端玩家的小清新的代码... 思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射       2.邻接表用两个vector存储,存储相邻关系       3.先尝试字母编号字典序最小的排列,此为next_permutation的最上排列       4.在最理想的情况下都不能得到比当前最优解更好的方案,则应当剪枝(prune)       5.memcpy(),strchr()方法来自于库函数 测试集: Input:      A:FB;B:GC