小编Joh*_*hnQ的帖子

迭代DFS与递归DFS和不同元素顺序

我编写了一个递归DFS算法来遍历图:

void Graph<E, N>::DFS(Node n)
{
    std::cout << ReadNode(n) << " ";

    MarkVisited(n);

    NodeList adjnodes = Adjacent(n);

    NodeList::position pos = adjnodes.FirstPosition();

    while(!adjnodes.End(pos))
    {
        Node adj = adjnodes.ReadList(pos);

        if(!IsMarked(adj))
            DFS(adj);

        pos = adjnodes.NextPosition(pos);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我用堆栈编写了迭代DFS算法:

template <typename E, typename N>
void Graph<E, N>::IterativeDFS(Node n)
{
    Stack<Node> stack;

    stack.Push(n);

    while(!stack.IsEmpty())
    {
        Node u = stack.Read();

        stack.Pop();

        if(!IsMarked(u))
        {
            std::cout << ReadNode(u) << " ";

            MarkVisited(u);

            NodeList adjnodes = Adjacent(u);

            NodeList::position pos = adjnodes.FirstPosition();

            while(!adjnodes.End(pos))
            {
                stack.Push(adjnodes.ReadList(pos));

                pos = adjnodes.NextPosition(pos);
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm traversal graph depth-first-search

40
推荐指数
1
解决办法
4万
查看次数

在8难题游戏中使用Python计算曼哈顿距离

我正在尝试用Python编写一个简单的A *求解器,以编写一个简单的8-Puzzle游戏。我以这种方式表示了我的游戏目标:

goal = [[1, 2, 3],
        [8, 0, 4], 
        [7, 6, 5]]
Run Code Online (Sandbox Code Playgroud)

我的问题是我不知道如何为目标编写简单的“曼哈顿距离”启发式方法。我知道应该将其定义为一般状态与目标状态之间的距离之和。我认为我应该编写类似以下内容的代码:

def manhattan_distance(state):
    distance = 0
    for x in xrange(3):
        for y in xrange(3):
            value = state[x][y]
            x_value = x
            y_value = y
            x_goal = ...?
            y_goal = ...?
            distance += abs(x_value - x_goal) + abs(y_value - y_goal)
    return distance
Run Code Online (Sandbox Code Playgroud)

我的问题是,我没有目标状态下各个部件的坐标的明确表示,因此我不知道如何为板的“价值”部件定义“ x_goal”和“ y_goal”。我正在尝试使用除法和模块操作来做到这一点,但这很困难。

您能给我一些提示来定义我的'x_goal'和'y_goal'变量吗?

谢谢

python puzzle search a-star

6
推荐指数
1
解决办法
3万
查看次数

在__add__运算符中返回相同子类的对象

我正在为自己的翻译开发一个简单的类型系统.我写的是这样的:

class Base(object):
    def __init__(self, content):
        self.__content = content

    @property
    def content(self):
        return self.__content

    @content.setter
    def content(self, value):
        self.__content = value

class Number(Base):
    def __init__(self, content):
        super(Number, self).__init__(content)

    def __add__(self, other):
        return Number(self.content + other.content)

    ...and so on

class Float(Number):
    def __init__(self, content):
        super(Float, self).__init__(content)

class Integer(Number):
    def __init__(self, content):
        super(Integer, self).__init__(content)
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我做这样的事情,很明显:

if __name__ == '__main__':
    f1 = Float(3.5)
    f2 = Float(2.3)
    f3 = f1 + f2
    type(f3)
Run Code Online (Sandbox Code Playgroud)

我总结了f1和f2,它们是Float类型,但我已经获得了f3,这是Number类型,但我希望f3是Float类型.我如何在我的Number超类中定义我的add运算符一次返回一个与f1和f2相同的类型?我有没有使用isinstance?有更清洁的方法吗?

谢谢!

python inheritance overloading operator-overloading

4
推荐指数
1
解决办法
1296
查看次数

在Runnable线程中的文件中写入换行符

我想写一个简单的类,每隔5秒打印一个文件.

我的代码是:

public class FileLoggerTask implements Runnable
{
    private PrintWriter printWriter = null;
    private boolean isActive = true;

    public FileLoggerTask(PrintWriter printWriter)
    {
        this.printWriter = printWriter;
    }

    public void run()
    {
        while(isActive)
        {
            printWriter.println("Test line");

            try
            {
                Thread.sleep(5000);
            }

            catch(InterruptedException e)
            {
                System.out.println(e);
            }
        }
    }

    void closeTask()
    {
        isActive = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在一个按钮的监听器中,我写道:

File file = new File("TestFile.txt");
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);
FileLoggerTask fileLoggerTask = new FileLoggerTask(printWriter);
Thread thread = new Thread(fileLoggerTask);
Run Code Online (Sandbox Code Playgroud)

在我写的另一个按钮的监听器中: …

java multithreading android printwriter

1
推荐指数
1
解决办法
339
查看次数