From the course: Code Clinic: C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Trying again and eventual success

Trying again and eventual success - C Tutorial

From the course: Code Clinic: C

Start my 1-month free trial

Trying again and eventual success

- [Instructor] As I pondered my initial failure with the N Queens program, I thought about how to fix it. I believe the approach to check for attacks is sound but the method of tracking a queen's attack was wrong, so I reversed the method. Instead of setting attacks, I'd be defensive and scan a square for potential attacks from elsewhere. The key to doing that is to find a queen's location on the board and the key to that is to store the queen's location specifically as a row, column pair. Then I thought about a two dimensional array, one element for the row and another for the column. Given that, the first element would always be sequential, row one through n and that's the same values as an element in an array. So all that's truly needed is a one dimensional array of integers. Each element gives the queen's column position for a certain row. Working with my original code I combined the set queen function with the find_square function. The function takes as an argument the board…

Contents