UVa140 Bandwidth

简介: UVa140 Bandwidth
#include <stdio.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#define MAXN 30#define BUFLEN 80#define STRLEN 30intgraph[MAXN][MAXN];
inthasChar[STRLEN];
intChar[STRLEN];
intlen;
intvis[STRLEN];
intmax;
intresult[STRLEN];
intans[STRLEN];
intcmp_char(constvoid*a, constvoid*b);
voiddfs(intcur);
intmain()
{
inti;
charbuf[BUFLEN];
intstart, end;
#ifndef ONLINE_JUDGEfreopen("d:\\uva_in.txt", "r", stdin);
#endifwhile (scanf("%s", buf) &&buf[0] !='#') {
memset(hasChar, -1, sizeof(hasChar));
memset(graph, 0, sizeof(graph));
memset(vis, 0, sizeof(vis));
max=100;
i=0;
len=0;
while (i<strlen(buf)) {
start=buf[i] -'A';
if (hasChar[start] ==-1) {
hasChar[start] =1;
Char[len++] =start;
            }
i+=2;
while (isalpha(buf[i])) {
end=buf[i] -'A';
if (hasChar[end] ==-1) {
hasChar[end] =1;
Char[len++] =end;
                }
graph[start][end] =graph[end][start] =1;
i++;
            }
i++;
        }
qsort(Char, len, sizeof(int), cmp_char);
dfs(0);
for (i=0; i<len; i++) {
printf("%c ", ans[i] +'A');
        }
printf("-> %d\n", max);
    }
return0;
}
intcmp_char(constvoid*a, constvoid*b)
{
constint*tmp_a= (constint*)a;
constint*tmp_b= (constint*)b;
return*tmp_a-*tmp_b;
}
voiddfs(intcur)
{
inti, j;
inttemp;
if (cur==len) {
temp=0;
for (i=0; i<cur-1; i++) {
for (j=i+1; j<cur; j++) {
if (graph[result[i]][result[j]] && (j-i) >temp)
temp=j-i;
            }
        }
if (temp<max) {
max=temp;
memcpy(ans, result, sizeof(result));
        }
    } else {
for (i=0; i<len; i++) {
if (!vis[i]) {
vis[i] =1;
result[cur] =Char[i];
dfs(cur+1);
vis[i] =0;
            }
        }
    }
}
目录
相关文章
|
14天前
POJ—2236 Wireless Network
POJ—2236 Wireless Network
|
9月前
UVa1531 - Problem Bee
UVa1531 - Problem Bee
35 0
|
9月前
uva101 The Blocks Problem
uva101 The Blocks Problem
32 0
Codeforces 954D. Fight Against Traffic
Codeforces 954D. Fight Against Traffic
52 0
uva live 3516 - Exploring Pyramids
点击打开链接 题意:给出一棵多叉树,每个结点的任意两个子节点都有左右之分。从根节点开始,每次尽量往左走,走不通就回溯,把遇到的字母顺序记录下来,可以得到一个序列。
744 0
uva 1329 Corporative Network
点击打开链接uva 1329 思路: 带权并查集 分析: 1 看懂题目就是切菜了 代码: #include #include #include #include using namespace std; const int MAXN...
864 0