题意描述:给定6个等边三角形,三角形的每条边都有权值,三个权值顺时针给出:
按照权值相等的边合并,问组成的等边6边形边长的权值最大;
#include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <stack> #include<algorithm> #include<set> using namespace std; #define inf 2147483647 #define eps 1e-8 #define LL long long #define M 50005 #define mol 1000000007 struct node//三角形的边 { int a,b,c; }p[7]; int sum,vis[7],z,tt,temp; int dfs(int x,int y,int step,int sum)//x为要重合的边,y为6边形外围的边,sum为当前权值和 { sum+=y; if(x==z&&step==6) { if(sum>temp) temp=sum; } for(int i=0;i<7;i++) { if(vis[i]==0) { if(p[i].a==x) { vis[i]=1; dfs(p[i].c,p[i].b,step+1,sum); vis[i]=0;//回溯 } if(p[i].b==x) { vis[i]=1; dfs(p[i].a,p[i].c,step+1,sum); vis[i]=0; } if(p[i].c==x) { vis[i]=1; dfs(p[i].b,p[i].a,step+1,sum); vis[i]=0; } } } return temp; } int main() { int a,b,c,i; char ch[2]; while(1) { int flag=0; sum=0;tt=0;temp=-1; memset(vis,0,sizeof(vis)); i=0; while(scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c)) { i++; if(i==6) { scanf("%s",ch); if(ch[0]=='$') flag=1; break; } } int ans=-1,ans1,ans2,ans3; i=0; vis[i]=1; z=p[i].b;//注意方向(顺时针) ans1=dfs(p[i].a,p[i].c,1,0); z=p[i].a; ans2=dfs(p[i].c,p[i].b,1,0); z=p[i].c; ans3=dfs(p[i].b,p[i].a,1,0); ans=max(max(ans1,ans2),ans3); if(ans!=-1) printf("%d\n",ans); else printf("none\n"); if(flag) break; } //system("pause"); return 0; }