标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2880 Accepted Submission(s): 1230
#include <iostream> #include <string.h> using namespace std; int a[15],vis[15]; int res; int abs(int x) { return x<0?-x:x; } void dfs(int dep,int sum) { if(sum>res) { return ; } if(dep==9) { res=sum; return ; } for(int i=1;i<=10;i++) { if(!vis[i]) { vis[i]=1; for(int j=i+1;j<=10;j++) { if(!vis[j]) { dfs(dep+1,sum+abs(a[i]-a[j])); break; } } vis[i]=0; } } } int main() { int T; cin>>T; while(T--) { for(int i=1;i<=10;i++) { int x; cin>>x; a[x]=i; } res=0x3f3f3f3f; memset(vis,0,sizeof(vis)); dfs(0,0); cout<<res<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5691151.html