大企業の面接でこんな質問を受けました。
このインターフェースがあるとしましょう:
interface ILabyrinth
{
// create a labyrinth of size 100*100 max. Rabbit and carrot are positioned at random locations within the maze.
void Init();
// try to move the rabbit in one direction. return true if the move was successful, returns false otherwise (there was a wall)
bool tryMove(Direction d);
// return true if carrot and rabbit are at the same place.
bool isSuccess();
}
ニンジンを見つけるためにウサギをどのように動かしますか? 到達したら停止するだけです。
私の解決策は、tryMove() 関数を使用してマウスをバックトラックする必要があることを除いて、従来の DFS 検索を適用することです。
そして、サイズ 198*198 ( = (100-1)*2) の新しいマップでウサギの動きを追跡することによってそれを行います。この地図では、私たちがその位置に来たときの方向を保存しています。簡単に後戻りできるので。
それを改善する方法はありますか?