SGU 502 Digits Permutation

  这个题目 正解应该是  dp 吧  对18个数字进行2进制枚举放不放,,,可以这么理解  以当前状态 stu,他对应的余数是 h 进入下一个状态; 进行记忆画搜索就行了
1
#include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 8 char str[20]; bool vis[20]; 9 int dp[1000000][20],len; int res[20]; 10 int dfs( int stu,int h,int dep ) 11 { 12 if( dep == len ){ if( h || str[res[0]] == '0' )return 0; 13 for( int i = 0; i < len; i++ )cout<<str[res[i]];cout<<endl; 14 return 1; 15 } 16 if( dp[stu][h] == 0 )return 0; 17 for( int i = 0; i < len; i++ ) 18 if( !vis[i] ){ 19 vis[i] = true;res[dep] = i; 20 int now = dfs( stu|(1<<i),((h*10)+(str[i]-'0'))%17,dep+1 ); 21 vis[i] = false; 22 if( now == 1 )return 1; 23 } 24 return dp[stu][h] = 0; 25 } 26 int main( ) 27 { 28 while( scanf("%s",str) != EOF ) 29 { 30 len = strlen( str ); 31 memset( dp,-1,sizeof(dp) ); 32 memset( vis,0,sizeof(vis) ); 33 if( dfs( 0,0,0 ) == 0 )cout<<"-1"<<endl; 34 } 35 return 0; 36 }