Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
偶暴力就没有过。。。TLE了,但是看了你的居然过了。好奇怪。In Reply To:应该是死循环了吧。。。我暴力都过了。。。 Posted by:Spacesheep at 2005-08-19 20:03:12 #include <iostream> using namespace std; bool used[20]; int matrix[20][20]; int n, result; bool valid () { int sum = 0; for ( int i = 0; i < n; i++ ) if ( used[i] ) sum++; return sum < n; } int caculate () { int sum = 0; for ( int i = 0; i < n; i++ ) { if ( used[i] ) { for ( int j = 0; j < n; j++ ) if ( !used[j] ) sum += matrix[i][j]; } } return sum; } void search ( int index ) { if ( !valid () ) return; if ( index == n ) { result = result > caculate () ? result : caculate (); return; } used[index] = true; search ( index + 1 ); used[index] = false; search ( index + 1 ); } int main() { int i, j; scanf ( "%d", &n ); for ( i = 0; i < n; i++ ) for ( j = 0; j < n; j++ ) scanf ( "%d", &matrix[i][j] ); result = -1; search ( 0 ); printf ( "%d\n", result ); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator