Code Signal_练习题_Minesweeper
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的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- 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) + ...
随机推荐
- Python(网络基础)
day33 参考:http://www.cnblogs.com/linhaifeng/articles/5937962.html IP协议: 规定网络地址的协议叫ip协议,它定义的地址称之为ip地址, ...
- 比特、字节、K
比特(bit) 比特,计算机专业术语,是信息量单位,由英文BIT音译而来.BIT为Binary digit(二进制数)位的缩写.二进制数的一位所包含的信息就是一比特,如二进制数0100就是4比特. 字 ...
- 基于jeesite+android开发 电子商务系统免费教程
下载地址: jeesite免费教程 基于jeesite+android开发 电子商务系统免费教程 基于jeesite+android开发 电子商务系统免费教程 这个教程已经录制完很久了,一直没有公开, ...
- jvm(1)类的加载(三)(线程上下文加载器)
简介: 类加载器从 JDK 1.0 就出现了,最初是为了满足 Java Applet 的需要而开发出来的. Java Applet 需要从远程下载 Java 类文件到浏览器中并执行. 现在类加载器在 ...
- out.print()与out.write()的区别
out对象的类型是JspWriter.JspWriter继承了java.io.Writer类. 1)print方法是子类JspWriter,write是Writer类中定义的方法: 2)重载的prin ...
- Swinject 源码框架(一):基本原理
 核心是 Container类.它提供了两类方法,register 和 resolve. 为了找到在 resolve 时,能够找到对应的方法,内部维护了一个叫做services 的字典.key 是根 ...
- docker私服registry管理镜像
前言 首先试想这样一个场景:当在自己的机器上(docker中)构建了mysql镜像,eureka镜像等等微服务镜像,这些镜像有可能需要放到其他的机器上docker环境中去运行,实行分布式架构部署.但如 ...
- OS之进程管理---进程调度和多线程调度
进程调度基本概念 多道程序的目标就是始终允许某个进程运行以最大化CPU利用率,多个进程通时存在于内存中,操作系统通过进程调度程序按特定的调度算法来调度就绪队列中的进程到CPU,从而最大限度的利用CPU ...
- GODADDY 优质DNS 未被墙
下面列出的是我本地测试出的优质服务器 NS10.DOMAINCONTROL.COM NS12.DOMAINCONTROL.COM NS14.DOMAINCONTROL.COM NS19.DOMAINC ...
- C/C++ -- Gui编程 -- Qt库的使用 -- Qt5总结
主要变化: 1.与Qt4相比,Qt5可以直接显示中文不需要手工转码2.Qt5模块发生变化,大部分组件并入widgets模块 注意事项:工程文件加一句greaterThan(QT_MAJOR_VERSI ...