Magnet Chess
InterviewTime limit1sMemory limit1024 MB
Given an N by N board of magnets and a touch distance D, find the empty cell where a new magnet touches the fewest existing magnets, or none if a cell touches zero.
- Level
Medium4 of 10
- Topics
- Prefix sum, Matrix, Brute force
- Solved
- No attempts yet
Problem
Sejong, who has come over to the Joseon era, decided to teach magnet chess to people who are tired of janggi and baduk.
Magnet chess is a game for two players. Each player holds the same number of magnets. The players take turns placing one magnet at a time on an empty cell of an board.
On a turn, a player places one of their magnets on an empty cell so that it does not touch any other magnet. The player who uses up all their magnets first wins. However, if the placed magnet touches other magnets, the player takes back all touching magnets, including the new one.
A cell in row and column is written , and the top-left cell is . Suppose a magnet is at and a new magnet is placed at . If and , the magnet at touches the new magnet.
Midway through the game, Sejong has one magnet left, and it is now Sejong's turn. Find the best position Sejong can place on this turn, to help Sejong, who has lost focus.
Input
The first line contains the board size and the touching distance limit , separated by a space. (, )
The next lines describe the board. A cell with a magnet is 1, and an empty cell is 0. The -th number on the -th line tells whether there is a magnet at row , column .
The initial board has no two magnets touching.
Output
On the first line, print the position where Sejong can win on this turn, if such a position exists. If no winning position exists, print the position that takes the fewest magnets. Print the row and column separated by a space.
On the second line, if no winning position exists, print the fewest number of magnets taken.
If several positions qualify, print the one with the smallest row number. If the row numbers are the same, print the one with the smaller column number.