Connect Two Game
In the Link Game, you can eliminate pairs of matching tiles, but only if the connection between them makes no more than two turns. If there are more than two turns, you cannot eliminate them.
In this game, you need to implement the connect
function. You will be given a board and the coordinates of two tiles: row1, col1, row2, col2
. Please check if these two tiles can be connected. If they can be connected with no more than two turns, return the path. Otherwise, return an empty result. See the problem for details:
The common solution for this problem is to use the BFS algorithm, but the scenarios in the Link Game are more complex than typical algorithm problems:
There is a "no more than two turns" rule.
You need to record the exact path connecting the two tiles.
In the Link Game, the connection may go outside the board if the tile is at the edge.
Try to solve it yourself first. I will show a sample solution below.