码迷,mamicode.com
首页 > 其他好文 > 详细

Code Signal_练习题_Minesweeper

时间:2018-08-12 23:44:00      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:div   false   off   signal   one   star   create   style   example   

In the popular Minesweeper game you have a board with some mines and those cells that don‘t contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

Example

For

matrix = [[true, false, false],
          [false, true, false],
          [false, false, false]]

the output should be

minesweeper(matrix) = [[1, 2, 1],
                       [2, 1, 1],
                       [1, 1, 1]]

Check out the image below for better understanding:

技术分享图片

 

不会做.....

 

 

技术分享图片
def minesweeper(matrix):
    r = []

    for i in range(len(matrix)):
        r.append([])
        for j in range(len(matrix[0])):
            l = -matrix[i][j]
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if 0 <= i + x < len(matrix) and 0 <= j + y < len(matrix[0]):
                        l += matrix[i + x][j + y]

            r[i].append(l)
    return r
膜拜大佬

 

Code Signal_练习题_Minesweeper

标签:div   false   off   signal   one   star   create   style   example   

原文地址:https://www.cnblogs.com/YD2018/p/9465391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!