Sit-In Lab 1 Ob-CHESS-ion N-Queens Puzzle Checker
Description: Sit-In Lab 1 Ob-CHESS-ion N-Queens Puzzle Checker Famous N-Queens puzzle You want to check if the input solution is correct No 2 queens can be on the same row, column or diagonal Write a piece of code to verify the solution Input: N
Related Topics
Download Presentation
"Sit-In Lab 1 Ob-CHESS-ion N-Queens Puzzle Checker" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
slide1. Sit-In Lab 1Ob-CHESS-ion<br>
slide2. N-Queens Puzzle Checker Famous N-Queens puzzle
You want to check if the input solution is correct
No 2 queens can be on the same row, column or diagonal
Write a piece of code to verify the solution
Input:
N – Number of rows ( Number of queens on chessboard )
Followed by N rows of N numbers
0 – Empty space
1 – Queen
Output whether the puzzle is a valid solution<br>
slide3. Example – Valid? Output :<br>
slide4. Example – Incorrect solution Output : INVALID<br>
slide5. Example – Valid Solution Output : VALID<br>
slide6. No other queen in same row, column and diagonal<br>
slide7. Algorithm Loop through each square of the array:
Start from the top left, iterate row by row
For each queen found
Starting from the square on its right, check if there exists another queen in the same row
Starting from the square right below it, check if there exists another queen in the same column
Starting from the square on its bottom right, check if there exists another queen in the same right diagonal
Starting from the square on its bottom left, check if there exists another queen in the same left diagonal
Return false if there exists another queen<br>
slide8. Visualisation – Start from top left<br>
slide9. Visualisation – Queen is found, check rows and diagonals<br>
slide10. Visualisation – Queen is found, check rows and diagonals Valid<br>
slide11. Visualisation – Iterate row by row - No queen in this space<br>
slide12. Visualisation – Another Queen is found<br>
slide13. Visualisation – Queen is found, check rows and diagonals Valid<br>
slide14. Visualisation – Iterate row by row<br>
slide15. Visualisation – Another Queen is found<br>
slide16. Visualisation – Queen is found, check rows and diagonals Invalid<br>
slide17. Visualisation – Break from loop and return false Invalid<br>
slide18. Main method:
Read in the inputs
Call helper methods and print the output
Helper methods:
Check rows, columns and diagonals ( 4 directions ), returning false if another queen is found
Check if given coordinates are out of array bounds ( >= N )<br>
slide19. How to check diagonals?<br>
slide20. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [1][3]<br>
slide21. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [2][4]<br>
slide22. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][5]<br>
slide23. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][5]
Row++
Col++<br>
slide24. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [1][3]<br>
slide25. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [2][2]<br>
slide26. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][1]<br>
slide27. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][1]
Row++
Col--<br>
slide28. Questions?<br>
slide2. N-Queens Puzzle Checker Famous N-Queens puzzle
You want to check if the input solution is correct
No 2 queens can be on the same row, column or diagonal
Write a piece of code to verify the solution
Input:
N – Number of rows ( Number of queens on chessboard )
Followed by N rows of N numbers
0 – Empty space
1 – Queen
Output whether the puzzle is a valid solution<br>
slide3. Example – Valid? Output :<br>
slide4. Example – Incorrect solution Output : INVALID<br>
slide5. Example – Valid Solution Output : VALID<br>
slide6. No other queen in same row, column and diagonal<br>
slide7. Algorithm Loop through each square of the array:
Start from the top left, iterate row by row
For each queen found
Starting from the square on its right, check if there exists another queen in the same row
Starting from the square right below it, check if there exists another queen in the same column
Starting from the square on its bottom right, check if there exists another queen in the same right diagonal
Starting from the square on its bottom left, check if there exists another queen in the same left diagonal
Return false if there exists another queen<br>
slide8. Visualisation – Start from top left<br>
slide9. Visualisation – Queen is found, check rows and diagonals<br>
slide10. Visualisation – Queen is found, check rows and diagonals Valid<br>
slide11. Visualisation – Iterate row by row - No queen in this space<br>
slide12. Visualisation – Another Queen is found<br>
slide13. Visualisation – Queen is found, check rows and diagonals Valid<br>
slide14. Visualisation – Iterate row by row<br>
slide15. Visualisation – Another Queen is found<br>
slide16. Visualisation – Queen is found, check rows and diagonals Invalid<br>
slide17. Visualisation – Break from loop and return false Invalid<br>
slide18. Main method:
Read in the inputs
Call helper methods and print the output
Helper methods:
Check rows, columns and diagonals ( 4 directions ), returning false if another queen is found
Check if given coordinates are out of array bounds ( >= N )<br>
slide19. How to check diagonals?<br>
slide20. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [1][3]<br>
slide21. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [2][4]<br>
slide22. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][5]<br>
slide23. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][5]
Row++
Col++<br>
slide24. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [1][3]<br>
slide25. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [2][2]<br>
slide26. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][1]<br>
slide27. 0 1 2 3 4 5 6 7 0
1
2 3
4
5
6
7 [3][1]
Row++
Col--<br>
slide28. Questions?<br>