Rush Hour - Solving the game

前端 未结 7 1830
太阳男子
太阳男子 2021-01-30 10:47

Rush Hour
if you\'re not familiar with it, the game consists of a collection of cars of varying sizes, set either horizontally or vertically, on a NxM grid

7条回答
  •  -上瘾入骨i
    2021-01-30 11:38

    I think recursion is a bad idea, unless you keep track of what you've already visited; you could end up recursing infinitely by moving a car back and forth.

    Maybe this is a good start: Represent and store each board state as an undirected graph. Then for each possible move, check against past states to make sure you're not just hitting the same state again.

    Now make another undirected graph where nodes represent states and edges represent the ability to get from one state to another via moving a car. Explore states until one of them is a solution. Then follow the edges back to the beginning to find out a move path.

提交回复
热议问题