gpt4 book ai didi

c# - 从广度优先搜索转换为深度优先有限搜索

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:47 26 4
gpt4 key购买 nike

作为所有搜索算法的新手,我正在研究一个老式的 8 题难题的例子,我已经完成了下面代码中的广度优先算法,我想知道如何将它转换为深度优先有限搜索。

如何从广度优先算法转换为深度优先有限搜索算法?

代码:

 class BFS
{
int[] GoalState = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
private Queue<Node> Frontier;
private HashSet<Node> Solution;
private Node RootNode;

public BFS(int[] StartState)
{
this.RootNode = new Node(StartState, -1, "\0");
Solution = new HashSet<Node>();
Frontier = new Queue<Node>();
}

public void Solve()
{
Node ActiveNode = RootNode;
bool IsSolved = false;
while (!IsGoalState(ActiveNode.GetState()))
{
Solution.Add(ActiveNode);
foreach (Node successor in GenerateSuccessor(ActiveNode))
{
Frontier.Enqueue(successor);
if (IsGoalState(successor.GetState()))
{
IsSolved = true;
Solution.Add(successor);
break;
}
}
if (IsSolved)
break;

ActiveNode = Frontier.Dequeue();
}
WriteSolution();
}

public IEnumerable<Node> GenerateSuccessor(Node ParentNode)
{
int[] ParentState = ParentNode.GetState();
int EmptySpacePosition = 0;
int temp;
for (int x = 0; x < 9; x++)
{
if (ParentState[x] == 0)
{
EmptySpacePosition = x;
break;
}
}

//Can move empty space to LEFT?
if (EmptySpacePosition != 0 && EmptySpacePosition != 3 && EmptySpacePosition != 6)
{
int[] _State = (int[])ParentState.Clone();
_State[EmptySpacePosition] = _State[EmptySpacePosition - 1];
_State[EmptySpacePosition - 1] = 0;
yield return new Node(_State, ParentNode.GetId(), "Left ");
}

//Can move empty space to RIGHT?
if (EmptySpacePosition != 2 && EmptySpacePosition != 5 && EmptySpacePosition != 8)
{
int[] _State = (int[])ParentState.Clone();
_State[EmptySpacePosition] = _State[EmptySpacePosition + 1];
_State[EmptySpacePosition + 1] = 0;
yield return new Node(_State, ParentNode.GetId(),"Right ");
}

//Can move empty space to UP?
if (EmptySpacePosition > 2)
{
int[] _State = (int[])ParentState.Clone();
_State[EmptySpacePosition] = _State[EmptySpacePosition - 3];
_State[EmptySpacePosition - 3] = 0;
yield return new Node(_State, ParentNode.GetId(), "Up ");
}

//Can move empty space to DOWN?
if (EmptySpacePosition < 6)
{
int[] _State = (int[])ParentState.Clone();
_State[EmptySpacePosition] = _State[EmptySpacePosition + 3];
_State[EmptySpacePosition + 3] = 0;
yield return new Node(_State, ParentNode.GetId(),"Down ");
}
}

public bool IsGoalState(int[] State)
{
for (int x = 0; x < 9; x++)
{
if (State[x] != GoalState[x])
return false;
}
return true;
}

public void WriteSolution()
{
StringBuilder s = new StringBuilder();
int ParentId = 0;

#region InfoPrint
Console.Write("Puzzle= ");
foreach (int i in RootNode.GetState())
{
Console.Write(i);
}
Console.WriteLine();
Console.WriteLine("Nodes Generated= " + (Solution.Count + Frontier.Count));

#endregion

foreach (Node n in Solution.Reverse())
{
if (ParentId == 0 || n.GetId() == ParentId)
{
s.Append(n.GetMove());
ParentId = n.GetParentId();
}
}

Console.WriteLine("Solution Length= " + (s.Length - 1));
Console.WriteLine("Solution= " + s.ToString());
}
}

这是我的类节点

class Node
{
static int _IdCnt = 0;
private int[] State;
private int Id;
private int ParentId;
private string Move;
public Node(int[] State, int ParentId, string Move)
{
Id = _IdCnt++;
this.State = State;
this.ParentId = ParentId;
this.Move = Move;
}

public void SetState(int[] State)
{
this.State = State;
}

public int[] GetState()
{
return (int[])State.Clone();
}

public int GetId()
{
return Id;
}

public int GetParentId()
{
return ParentId;
}

public string GetMove()
{
return Move;
}
}

最佳答案

在 DFS 中,您需要使用 stack 而不是 queue。基本上,您将根节点添加到堆栈中,当堆栈包含一个节点时,您弹出该节点,执行您的逻辑,将其邻居添加到堆栈中并继续。

1. Add root to a stack.
2. while(stack.Count > 0)
{
3. pop the stack
4. if matches, return
5. else add neighbors to stack
}

return not found

关于c# - 从广度优先搜索转换为深度优先有限搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com