/// Raihan Ruhin /// CSE, Jahangirnagar University. /// Dhaka-Bangladesh. /// id: raihanruhin (topcoder / codeforces / codechef / uva), 3235 (lightoj) /// mail: raihanruhin@ (yahoo / gmail / facebook) /// blog: ruhinraihan.blogspot.com #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) { //left for(int i=0;i<col;i++) if(board[row][i]) return false; //upper left for(int i=row, j=col;i>=0 && j>=0;i--, j--) if(board[i][j]) return false; //bottom left 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<<"Case "<<kk++<<": "<< mx <<"\n"; cout<<setw(5)<<mx<<"\n"; } return 0; }
Friday, 7 August 2015
UVA 167 The Sultan's Successors (UVALive 5227, SCU 1033, HDU 1642, Regionals 1991 >> South Pacific)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment