1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <bits/stdc++.h> #define ll long long #define int long long #define double long double #define N 10 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define rrep(i,l,r) for(int i=l;i<r;i++) #define pii pair<int,int> using namespace std; int n,chess[N][N],Q[N],ans=0;
bool check(int row,int col){ for(int i=1;i<row;i++){ if(col == Q[i]||abs(row-i)==abs(col-Q[i]))return 0; } return 1; }
void Queen(int row){ if(row>8){ int sum = 0; rep(i,1,8)sum += chess[i][Q[i]]; ans = max(ans,sum); return; } for(int i=1;i<=8;i++){ if(check(row,i)){ Q[row] = i; Queen(row+1); } } }
void solve(){ memset(chess,0,sizeof(chess)); memset(Q,0,sizeof(Q)); ans = 0; for(int i=1;i<=8;i++){ for(int j=1;j<=8;j++){ cin>>chess[i][j]; } } Queen(1); cout<<right<<setw(5)<<ans<<endl; }
signed main(){ int t;cin>>t; while(t--){ solve(); } }
|