uva 140

思路:暴力+剪枝

uva140

wa了好多次……数组开小了……!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std;
const int INF = 0xffffff;
const double esp = 10e-;
const double Pi = * atan(1.0);
const int Maxn = ;
const int mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
//int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}}; bool graph[Maxn][Maxn];
int arr[Maxn];
int n;
int Min;
int tot;
bool visit[Maxn];
int ans[Maxn];
int init[Maxn]; void dfs(int cur){
if(tot >= Min)
return;
if(cur == n){
Min = min(Min,tot);
for(int i = ;i < n;i++){
ans[i] = arr[i];
}
return;
}
for(int i = ;i < n;i++){
if(!visit[ init[i] ]){
visit[init[i]] = ;
arr[cur] = init[i];
int tmp = ;
for(int j = ;j < cur;j++){
if(graph[ arr[j] ][ init[i] ]){
int tt = abs(cur - j);
tmp = max(tmp,tt);
if(tmp > Min)
break;
}
}
int tt = tot;
tot = max(tmp,tot);
dfs(cur+);
visit[init[i]] = ;
tot = tt;
}
}
}
char str[];
int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
while(scanf("%s",str) != EOF){
if(str[] == '#')
break;
int len = strlen(str);
n = ;
memset(graph,,sizeof(graph));
memset(arr,,sizeof(arr));
memset(visit,,sizeof(visit));
for(int i = ;i < len;i++){
if(str[i] == ' ')
continue;
int a = str[i] - 'A';
if(!arr[a]){
init[n++] = str[i] - 'A';
arr[a] = ;
}
for(i = i+;str[i] != ';' && i < len;i++){
if(!isalpha(str[i]))
continue;
int b = str[i] - 'A';
graph[a][b] = ;
graph[b][a] = ;
if(!arr[b]){
init[n++] = str[i] - 'A';
arr[b] = ;
}
}
}
Min = INF;
tot = ;
sort(init,init+n);
dfs();
for(int i = ;i < n;i++){
printf("%c ",ans[i] + 'A');
}
printf("-> %d\n",Min);
}
return ;
}
上一篇:QQl聊天消息


下一篇:UVA 140 (13.07.29)