题目:
"Animals and plants can reproduce themselves, but it was only recently shown that machines can be made which also reproduce themselves……. Other kinds of self-reproducing machines will be described, and one simple mechanical model, with no electrical or magnetic complications, will be there in working order for the audience to inspect and operate." -- Edward Forrest Moore In cellular automata, the Moore neighborhood comprises the eight cells surrounding
a central cell on a two-dimensional square lattice. The neighborhood is named
after Edward F. Moore, a pioneer of cellular automata theory. Many board games
are played with a rectangular grid with squares as cells. For some games, it is
important to know about the conditions of neighbouring cells for chip (figure,
draught etc) placement and strategy.
You are given a state for a rectangular board game grid with chips in a binary
matrix, where 1 is a cell with a chip and 0 is an empty cell. You are also give
n the coordinates for a cell in the form of row and column numbers (starting from
0). You should determine how many chips are close to this cell.
Every cell interacts with its eight neighbours; those cells that are horizontally,
vertically, or diagonally adjacent.
For the given examples (see the schema) there is the same grid: ((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),) For the first example coordinates of the cell is (1, 2) and as we can see from the schema this cell has 3 neighbour chips. For the second example coordinates is (0, 0) and this cell contains a chip, but we count only neighbours and the answer is 1. Input: Three arguments. A grid as a tuple of tuples with integers (1/0), a row number and column number for a cell as integers. Output: How many neighbouring cells have chips as an integer. Example: count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 1, 2) == 3 count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 0, 0) == 1
题目很长,但看懂了其实还是很简单的,不多说,贴代码:
def count_neighbours(grid, row, col): # 新建两个空列表保存有效数据 xValue = [] yValue = [] # x的范围 listRangeX = range(0,len(grid)) # y的范围 listRangeY = range(0,len(grid[0])) # 周围所有坐标的X取值范围 rangeX = [row-1,row,row+1] # 周围所有坐标的y取值范围 rangeY = [col-1,col,col+1] # 筛选有效地x,y值 for x in rangeX: if x in listRangeX: xValue.append(x) for y in rangeY: if y in listRangeY: yValue.append(y) # 计算筛选出的坐标中值为1的个数 count = 0 for i in xValue: for j in yValue: if grid[i][j] == 1: count += 1 # 去除本点为一的情况 if grid[row][col] == 1: count -= 1 return count if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 1, 2) == 3, "1st example" assert count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 0, 0) == 1, "2nd example" assert count_neighbours(((1, 1, 1), (1, 1, 1), (1, 1, 1),), 0, 2) == 3, "Dense corner" assert count_neighbours(((0, 0, 0), (0, 1, 0), (0, 0, 0),), 1, 1) == 0, "Single"
最后找一下差距吧,看看python真正的强大:
def count_neighbours(grid, row, col):
#rows x的取值范围rows = range(max(0, row - 1), min(row + 2, len(grid))) #cols y的取值范围cols = range(max(0, col - 1), min(col + 2, len(grid[0]))) return sum(grid[r][c] for r in rows for c in cols) - grid[row][col]