#include<bits/stdc++.h>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)
#define MOD 1000000007
#define MX 100010
int mat[10][10], mx, n;
bool board[10][10];
bool isSafe(int row, int col)
{
for(int i=0;i<col;i++)
if(board[row][i]) return false;
for(int i=row, j=col;i>=0 && j>=0;i--, j--)
if(board[i][j]) return false;
for(int i=row, j=col;i<n && j>=0;i++, j--)
if(board[i][j]) return false;
return true;
}
void solveNQ(int col)
{
if(col>=n)
{
int sum=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(board[i][j])
sum+=mat[i][j];
mx=max(mx, sum);
}
for(int i=0;i<n;i++)
if(isSafe(i, col))
{
board[i][col]=true;
solveNQ(col+1);
board[i][col]=false;
}
return;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int kk=1, tc, m;
n=8;
string s;
cin>>tc;
while(tc--)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
CLR(board);
mx=0;
solveNQ(0);
cout<<setw(5)<<mx<<"\n";
}
return 0;
}
No comments:
Post a Comment