data-structures · intermediate · ~15 min

Queen-safe check

Test whether a queen placement is attacked.

Challenge

Implement:

int queens_safe(const int *cols, int row, int col);

cols[r] is the column of the queen already placed in row r, for rows 0..row-1. Return 1 if placing a queen at (row, col) attacks none of them, else 0.

Input format

cols (earlier placements), row, col.

Output format

1 if safe, else 0.

Constraints

Check same column and both diagonals.

Starter code

#include <stddef.h>
/* cols[r] is the column of the queen in row r, for rows 0..row-1. Return 1 if a queen at (row,col) attacks none of them. */
int queens_safe(const int *cols,int row,int col){ (void)cols;(void)row;(void)col; return 1; }

Common mistakes

Only checking the column, not the diagonals (equal row/column distance).

Edge cases to handle

No earlier queens (row==0) is always safe.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.