answersLogoWhite

0

AllQ&AStudy Guides
Best answer

def queens_collide?(i, j, col, diag_plus, diag_minus) col.include?(j) diag_plus.include?(i + j) diag_minus.include?(i - j) end def place_queens(n, i, j, col, diag_plus, diag_minus) if i.zero? col elsif j.zero? false elsif queens_collide?(i, j, col, diag_plus, diag_minus) place_queens(n, i, j - 1, col, diag_plus, diag_minus) else queen_placed_ok = place_queens( n, i - 1, n, col + [j], diag_plus + [i + j], diag_minus + [i - j] ) if queen_placed_ok queen_placed_ok else place_queens(n, i, j - 1, col, diag_plus, diag_minus) end end end def queens(n) place_queens(n, n, n, [], [], []) end puts queens(4).inspect # >> [3, 1, 4, 2]

This answer is:
Related answers

def queens_collide?(i, j, col, diag_plus, diag_minus) col.include?(j) diag_plus.include?(i + j) diag_minus.include?(i - j) end def place_queens(n, i, j, col, diag_plus, diag_minus) if i.zero? col elsif j.zero? false elsif queens_collide?(i, j, col, diag_plus, diag_minus) place_queens(n, i, j - 1, col, diag_plus, diag_minus) else queen_placed_ok = place_queens( n, i - 1, n, col + [j], diag_plus + [i + j], diag_minus + [i - j] ) if queen_placed_ok queen_placed_ok else place_queens(n, i, j - 1, col, diag_plus, diag_minus) end end end def queens(n) place_queens(n, n, n, [], [], []) end puts queens(4).inspect # >> [3, 1, 4, 2]

View page

The code is below and i should also explain the algorithm. Well, What we are doing here is that we already defined the size to be 9x9 sudoku and are getting values using loops. All the empty spots are given UNASSIGNED value. Then we have functions to tell that if it is safe to put a value in the empty box by calculation and according to the rules of Sudoku it checks for is there any other some number horizontally and vertically and do the sum of the row and column is less than or equal to required or not. If the functions returns true then the program puts the value there.

  1. #include
  2. #define UNASSIGNED 0
  3. #define N 9
  4. bool FindUnassignedLocation(int grid[N][N], int &row, int &col);
  5. bool isSafe(int grid[N][N], int row, int col, int num);

  6. bool SolveSudoku(int grid[N][N])
  7. {
  8. int row, col;

  9. if (!FindUnassignedLocation(grid, row, col))
  10. return true; // success!

  11. for (int num = 1; num <= 9; num++)
  12. {

  13. if (isSafe(grid, row, col, num))
  14. {

  15. grid[row][col] = num;

  16. if (SolveSudoku(grid))
  17. return true;

  18. grid[row][col] = UNASSIGNED;
  19. }
  20. }
  21. return false;}

  22. bool FindUnassignedLocation(int grid[N][N], int &row, int &col)
  23. {
  24. for (row = 0; row < N; row++)
  25. for (col = 0; col < N; col++)
  26. if (grid[row][col] true)
  27. printGrid(grid);
  28. else
  29. printf("No solution exists");
  30. return 0;
  31. }






View page

#include<stdio.h>

unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) {

unsigned sum, col;

sum = 0;

for (col=0; col<width; ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) {

unsigned sum, row;

sum = 0;

for (row=0; row<width; ++row)

sum += sq[row*width+col];

return sum;

}

unsigned sum_diag (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=0; row<width; ++row, ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_anti (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=width-1; row<width; ++row, --col)

sum += sq[row*width+col];

return sum;

}

bool is_magic (unsigned* sq, const unsigned width) {

unsigned magic, row, col;

magic = sum_row (sq, width, 0);

for (row=1; row<width; ++row)

if (magic!=sum_row(sq, width, row))

return false;

for (col=0; col<width; ++col)

if (magic!=sum_col(sq, width, col))

return false;

if (magic!=sum_diag(sq, width))

return false;

if (magic!=sum_anti(sq, width))

return false;

return true;

}

int main () {

const unsigned width = 3;

unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}};

unsigned row, col;

printf ("Square:\n\n");

for (row=0; row<width; ++row) {

for (col=0; col<width; ++col) {

printf ("%d ", a[row][col]);

}

printf ("\n");

}

printf ("\n");

if (is_magic((unsigned*)&a, width))

printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0));

else

printf ("The square is not magic\n");

return 0;

}

View page

To Find the number in that matrix and check that number adjacency elements...

import java.util.Scanner;

public class FindAdjacencyMatrix {

public static int[][] array1 = new int[30][30];

public static int i,j,num,m,n;

public static void main(String args[]) {

Scanner input = new Scanner(System.in);

//-------------------------------------------------------------------------------------------------

System.out.println("Enter the m ,n matrix");

m = input.nextInt();

n = input.nextInt();

//-------------------------------------------------------------------------------------------------

System.out.println("Enter the matrix Element one by one:");

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

array1[i][j] = input.nextInt();

}

}

System.out.println("The Given Matrix is :");

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

System.out.print(" "+array1[i][j]);

}

System.out.print("\n");

}

//-------------------------------------------------------------------------------------------------

System.out.println("Find The Adjacency Elements for Given Number : ");

System.out.println("Enter The Number : ");

num = input.nextInt();

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

if(num == array1[i][j]) {

System.out.println("Element is Found :"+num);

findAdjacency(num,i,j);

break;

}

}

}

//--------------------------------------------------------------------------------------

}

private static void findAdjacency(int elem,int row,int col) {

try {

if( array1[row][col-1]!=-1) {

System.out.println("Left Adjacency : "+array1[row][col-1]);

}

} catch(Exception e){

System.out.println(" Exception Throwing ");

}

try{

if(array1[row][col+1]!= -1) {

System.out.println("Right Adjacency : "+array1[row][col+1]);

}

}catch(Exception e){

System.out.println(" Exception Throwing ");

}

try {

if(array1[row-1][col]!= -1) {

System.out.println("Top Adjacency : "+array1[row-1][col]);

}

} catch(Exception e){

System.out.println(" Exception Throwing ");

}

try {

if(array1[row+1][col]!= -1) {

System.out.println("Botto Adjacency : "+array1[row+1][col]);

}

} catch(Exception e){

System.out.println(" Exception Throwing ");

}

}

//----------------------------------------------------------------------------------------------

}

View page

Anthony Del Col is 6' 1".

View page
Featured study guide
📓
See all Study Guides
✍️
Create a Study Guide
Search results