This is an interview question:
For a matrix, we define an operation when, when adding 1 to one record, all surrounding records (up, down, left, right) will also be added 1. To obtain a positive matrix, we will find an algorithm for determining whether it is possible to construct a matrix from a zero matrix using such operations.
What is an effective algorithm to solve the issue?
I am currently thinking of using backtracking to try all possible combinations, however this is definitely inefficient. The question seems to be like the game "Lights", but here it is not 0/1, which is complicated.
Thank.
Edit:
For instance:
3 3 can be constructed from 0 0 -> 1 1 -> 2 2 -> 3 3
1 2 0 0 1 0 1 1 1 2
source
share