ARTS Week 4

Nov 18, 2019 ~ Nov 24, 2019

Algorithm

深入优先搜索-马遍历棋盘
要求:给定一个n * m的棋盘,左上角为(0, 0),马的初始位置为(x, y),找到所有方案使得马不重复地遍历棋盘,输出所有方案

思路

马走‘日’字,因此有八种移动方案,分别为:

dir = [[-2,1], [-1,2], [1,2], [2,1], [2,-1], [1,-2], [-1,-2], [-2,-1]]

坐标(x, y)移动一步后的结果为(x+dir[i][0], y+dir[i][1])
马的移动需要受到以下两个条件的约束:

  1. 移动后的点仍要在棋盘上,即
    $ 0 \leq x+dir[i][0] \leq n-1 $
    $ 0 \leq y+dir[i][1] \leq m-1 $
  2. 移动后的点是要为访问过的,即
    $ visited[x+dir[i][0]][y+dir[i][1]] $

    实现

    Python的实现代码如下:
def draw():
    print('第', ans,'种方案:',history)

def horse(x, y, step):
    if step == num:
        global ans, found
        ans = ans + 1
        found = True
        draw()
    else:
        for i in range(8):
            x_temp = x + dir[i][0]
            y_temp = y + dir[i][1]
            if 0 <= x_temp < n and 0 <= y_temp < m and visited[x_temp][y_temp] == 0:
                history[step] = [x_temp, y_temp]
                visited[x_temp][y_temp] = 1
                horse(x_temp, y_temp, step+1)
                visited[x_temp][y_temp] = 0

Review

Emacs is Sexy

文章介绍的Emacs好处和安装Emacs

  1. Why
  1. 安装
  1. 学习
    阅读自带的教程``或者查看手绘图
    中文教程可以查看子龙山人的教程,链接

希望能开箱即用的用户,可以采用Spacemacs

Tips

python3 输入一行数字中间用空格相隔

#方法1
num = [int(n) for n in input().split()]

#方法二
num = list(map(int, input().strip().split()))

print(num)

Share

我在尝试安装Spacemacs遇到了一些问题,所以写了一篇文章来讲述如何安装Spacemacs。
Spacemacs安装-链接

猜你喜欢

转载自www.cnblogs.com/mengxinayan/p/11973095.html