“HAPPINESS SHOULD BE A FUNCTION WITHOUT ANY PARAMETERS”

WEEK:12 PROGRAMMING ASSIGNMENT 2 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020



  

               An Introduction To Programming Through C++


IIT Bombay.

Week 12- Programming Assignment 2:




#include <bits/stdc++.h>
using namespace std;
void printArray(int board[][10], int n)
{
 for(int i=0; i<n; i++)
 {
  for(int j=0; j<n; j++)
  {
   if( board[i][j])
   {
    cout <<j;
   }
  }
 }
 cout << endl;
}
bool isSafe(int board[][10], int i, int j, int n)
{
 for(int row=0; row<i; row++)
 {
  if ( board[row][j] )
  {
   return false;
  }
 }
 int x=i, y=j;
 while(x>=0 && y>=0)
 {
  if( board[x][y] )
  {
   return false;
  }
  x--;
  y--;
 }
 x=i;
 y=j;
 while(x>=0 && y<=n-1)
 {
  if( board[x][y] )
  {
   return false;
  }
  x--;
  y++;
 }
 return true;
}
bool solveNQueen(int board[][10], int i, int n)
{
 if(i==n)
 {
  printArray(board, n);
  return false;
 }
 for(int j=0; j<n; j++)
 {
  if( isSafe( board, i, j, n) )
     {
   board[i][j]=1;
   bool nextQueenRakhPaye= solveNQueen(board, i+1, n);
   if( nextQueenRakhPaye )
   {
    return true;
   }
   board[i][j]=0;
     }
 }

 return false;
 
}


void appendnext(set<vector<int> >& confQueens, vector<int> pos, int n)
{
  int board[10][10]={0};
  solveNQueen(board, 0, n);
   exit(0);
}

FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a comment

If you have any problems related to solutions or any concept please let me know.