标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1461 Accepted Submission(s): 563
#include<cstdio> #include<math.h> #include<stdlib.h> #include<string.h> using namespace std; int s[13]; int ans=99999; int vis[20]; void dfs(int t,int sum ) { int i,j; if(sum>ans) return; if(t==9) { ans=sum; return; } for(i=1;i<=10;i++) { if(!vis[i]) { vis[i]=1; for(j=i;j<=10;j++)//要保证i<j比如说7先排到8后面,现在排5,前面1,2,3,4都没排的,就会出现5排到1后面。 { if(!vis[j]) { dfs(t+1,sum+abs(s[j]-s[i])); break; } } vis[i]=0; } } } int main() { int q,x; scanf("%d",&q); while(q--) { ans=9999999; memset(vis,0,sizeof(vis)); for(int i=1;i<=10;i++) { scanf("%d",&x); s[x]=i; } dfs(0,0); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/angledamon/p/3870844.html