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):
ret = []
w = len(matrix[0])
dic = {True:1, False:0}
newMatrix = []
newMatrix.append([])
newMatrix.append([])
for x in range(len(matrix[0])+2):
newMatrix[0].insert(0,False)
newMatrix[1].append(False)
for y in matrix:
y.append(False)
y.insert(0,False)
newMatrix.insert(-1,y) for i in range(len(matrix)):
ret.append([])
for j in range(w):
print(newMatrix[i+2][j+2])
k = int(dic[newMatrix[i][j]]) + int(dic[newMatrix[i][j+1]]) + int(dic[newMatrix[i][j+2]]) + int(dic[newMatrix[i+1][j]]) + int(dic[newMatrix[i+1][j+2]]) + int(dic[newMatrix[i+2][j]]) + int(dic[newMatrix[i+2][j+1]]) + int(dic[newMatrix[i+2][j+2]])
ret[i].append(k)
return ret
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的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

随机推荐

  1. JavaScript基础函数和词法分析以及常用的内置对象和使用方法(4)

    day52 参考:https://www.cnblogs.com/liwenzhou/p/8004649.html 函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式 ...

  2. zoj4020 Traffic Light(bfs+状态压缩)

    题意:每个点有两种状态,0/1,0表示只能上下方向走,1表示只能左右方向走.每走一步整个图的状态改变一次(即0->1,1->0). 数据范围:n,m<=1e15 开始迷之因为数组太大 ...

  3. day 46 Django 学习3 数据库单表操作以及反向解析

    前情提要: Django 已经学了不少了, 今天学习链接数据库的操作.以及相关的反向解析等 一:反向解析 1:反向解析模板层 跳转时设定url会随着前面的路由改变而改变         2:反向解析之 ...

  4. POJ 2370

    //我的解题思路是先把输入的含有n个元素的数组a排序(从小到大),然后对前(n+1)/2个元素作如下的处理, //s+= (a[i]+1)/2 #include <iostream> #i ...

  5. linux初始

    运维 IT运维,指IT公司中,运行和维护服务器的工作 核心工作: ​ 数据不能丢失 ​ 7*24小时运行 ​ 提高用户访问效率 一句换 管服务器的 服务器 要管服务器那就得先了解服务器 服务器也称为伺 ...

  6. (转)Python——functools

    原文:https://www.cnblogs.com/Security-Darren/p/4168310.html#t7 http://www.wklken.me/posts/2013/08/18/p ...

  7. Maven 学习笔记(一)

    什么是 maven? 对于已经使用过 maven 的用户来说 maven 就是一个架构工具,使用它可以将代码构建成一个可发布的工具.当然也有人会说maven是一个项目管理的工具.当然各有各的说法,那么 ...

  8. nginx的gzip模块

    gzip模块是我们在nginx里面经常用到的,压缩响应的数据,这通常有助于将传输数据的大小减少一半甚至更多.可以让我们访问网站更为流畅. Syntax Default Context gzip on ...

  9. Go 压测

    1. 单测 + 压测 压测 go test -bench=. -benchmem 单元测试 go test -v . 2. pprof + 火焰图(查看cpu占用,内存占用) 嵌入代码 import ...

  10. Oracle数据库 中的基础的一些语法结构

    方括号里的内容为可选项 大括号是必填 1PL/SQL结构块 DECLARE /* * 声明部分——定义常量.变量.复杂数据类型.游标.用户自定义异常 */ BEGIN /* * 执行部分——PL/SQ ...