data-structures · intermediate · ~15 min
Test whether a queen placement is attacked.
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.
cols (earlier placements), row, col.
1 if safe, else 0.
Check same column and both diagonals.
#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; }
Only checking the column, not the diagonals (equal row/column distance).
No earlier queens (row==0) is always safe.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.