Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int board[10][10];
- int queen[20],mx;
- int column[20],diagonal1[40],diagonal2[40];
- void Eight_Queen(int _at,int n)
- {
- int i,sum;
- if(_at==n+1)
- {
- sum = 0;
- for(i=1; i<=n; i++)
- {
- sum+=board[i][queen[i]];
- }
- mx = max(mx,sum);
- return ;
- }
- for(i=1; i<=n; i++)
- {
- if(column[i] || diagonal1[i+_at] || diagonal2[n+i-_at])
- continue;
- queen[_at] = i;
- column[i] = diagonal1[i+_at] = diagonal2[n+i-_at] = 1;
- Eight_Queen(_at+1,n);
- column[i] = diagonal1[i+_at] = diagonal2[n+i-_at] = 0;
- }
- }
- int main()
- {
- int test,i,j;
- scanf("%d",&test);
- while(test--)
- {
- for(i=1;i<=8;i++)
- {
- for(j=1;j<=8;j++)
- {
- scanf("%d",&board[i][j]);
- }
- }
- mx = -1;
- Eight_Queen(1,8);
- printf("%5d\n",mx);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment