Chess Puzzle
InterviewTime limit1sMemory limit128 MB
For each board, read the rook coordinates and report SAFE if no two rooks share a row or column, otherwise NOT SAFE.
- Level
Easy2 of 10
- Topics
- Implementation, Hash map, Brute force, Array
- Solved
- No attempts yet
Problem
You want to place several rooks on a chessboard. A rook moves along its own row and column, so for no two rooks to threaten each other, no two rooks may share the same row or the same column.
Given a placement of rooks, write a program that determines whether any rook threatens another.
Input
The chessboard is an board whose squares range from to .
The first line contains the number of chessboards .
Each of the following lines describes one chessboard. Each line starts with the number of rooks , followed by the column and row of each rook in order. In other words, each line contains pairs of (column, row), that is integers.
Output
For each chessboard, print SAFE if no rook threatens another, or NOT SAFE otherwise, on its own line.