any one kindly help me.........getting frequent wa's
Code: Select all
#include<stdio.h>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#define max 3000
#define pb(x) push_back(x)
#define valid(i,j,x,y) (i>=0 && i<x &&j<y && j>=0 )
using namespace std;
int no=1,x,y;
const int N = 8;
int position[N],arr[92][18];
int sum,mmax,ara[20][20],kk;
// Check if a position is safe
bool isSafe(int queen_number, int row_position)
{
// Check each queen before this one
// cout<<" "<<queen_number<<endl;
for(int i=0; i<queen_number; i++)
{
// Get another queen's row_position
int other_row_pos = position[i];
// Now check if they're in the same row or diagonals
if(other_row_pos == row_position || // Same row
other_row_pos == row_position - (queen_number-i) || // Same diagonal
other_row_pos == row_position + (queen_number-i)) // Same diagonal
return false;
}
return true;
}
// Recursively generate a tuple like [0 0 0 0], then [0 0 0 1] then etc...
void solve(int k)
{
if(k == N) // We placed N-1 queens (0 included), problem solved!
{
// Solution found!
// cout << "Solution: "<<no++<<" ";
sum=0;
// cout<<"comb "<<kk<<" ;";
for(int i=0; i<N; i++)
{
arr[kk][i]=position[i];
// cout<<position[i]+1<<" ";
}
kk++;
// cout << endl;
}
else
{
for(int i=0; i<N; i++) // Generate ALL combinations
{
// Before putting a queen (the k-th queen) into a row, test it for safeness
if(isSafe(k,i))
{
position[k] = i;
// Place another queen
solve(k+1);
}
}
}
}
int main()
{
#ifndef ONLINE JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE
int kase,flag=0;
solve(0);
scanf("%d",&kase);
for (; kase-- ; )
{
int sum=1;
mmax=0;
if(flag)
cout<<endl;
flag=1;
(scanf("%d %d",&x,&y));
printf("SOLN COLUMN\n");
printf(" # 1 2 3 4 5 6 7 8\n\n");
{
for (int i=0;i<kk ; i++)
{
if(arr[i][x-1]==y-1)
{
// printf("%2d ",pos1++);
printf("%2d ",sum++);
for (int j=0;j<8 ;j++ )
cout<<" "<<arr[i][j]+1;
cout<<endl;
}
}
;
}
// if(kase)
// cout<<endl;
;
}
return 0;
}