Queen

#include <iostream> using namespace std; int a[20],n = 8,b[50],c[50],d[20]; int chess[9][9]; int tong, m; void init() { for(int i=2;i<=2*n;i++) b[i]=1; for(int i=1-n;i<=n-1;i++) c[i]=1; for(int i=1;i<=n;i++) d[i]=1; } void output() { for(int i=1;i<=n;i++) cout << "{" << i << "," << a[i] << "} "; cout << endl; } int sum() { int tong=0; for(int i = 1; i<=8; i++) tong = tong + chess[i][a[i]]; return tong; } void tim(int i) { for(int j=1;j<=n;j++) { if(d[j]&&b[i+j]&&c[i-j]) { d[j]=0;b[i+j]=0;c[i-j]=0; a[i] = j; if(i==n){ if( sum () > m) m = sum(); }else tim(i+1); d[j]=1;b[i+j]=1;c[i-j]=1; } } } int main() { int T,k; //freopen("input.txt","r",stdin); cin >> T; for(int tc = 1; tc<=T; tc++) { cout<<"Case #"<<tc<<endl; cin >> k; for(int r = 0; r < k ; r++) { for(int i = 1; i <= 8; i++) for(int j = 1; j <= 8; j++) cin >> chess[i][j]; init(); tong = 0; m = 0; tim(1); cout << m << endl; } } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.