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) + ...
随机推荐
- Recursion-687. Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- Array-Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- Spring Boot快速搭建Web工程
先想一下,正常我们想要创建一个web服务,首先需要下载tomcat,创建web工程,配置各种web.xml,引入spring的配置,各种配置文件一顿倒腾.....下载有了spring boot,你创建 ...
- Google Guava 类库简介
Guava 是一个 Google开发的 基于java的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency librar ...
- java在编译期和运行期都做了什么
Java对象内存存储,引用传递,值传递详细图解 java对象在内存中的分配 编译过程: 编译器把一种语言规范转化为另一种语言规范的这个过程需要哪些步骤?回答这个问题需要参照<编译原理>,总 ...
- 如何通过Python暴力破解网站登陆密码
首先申明,该文章只可以用于交流学习,不可以用于其他用途,否则后果自负. 现在国家对网络安全的管理,越来越严,但是还是有一些不法网站逍遥法外,受限于国内的人力.物力,无法对这些网站进行取缔. 今天演示的 ...
- 使用R进行分组统计
分组统计数据集是很常见的需求,R中也有相应的包支持数据集的分组统计.自己尝试了写了段R代码来完成分组统计数据集,支持公式,感觉用起来还算方便.代码分享在文章最后. 使用方式: step 1: sour ...
- 多线程编程,CPU是如何解决多线程内存访问问题的
CPU对内存变量的修改是先读取内存数据到CPU Cache中,然后再由CPU做运算,运算完成后继续写入到内存中 在单核CPU中,这完全没有问题,然而在多核CPU中,每一个CPU核心都拥有自己独立的Ca ...
- Elasticsearch Aggregation 多个字段分组统计 Java API实现
现有索引数据: index:school type:student --------------------------------------------------- {"grade&q ...
- Jmeter环境搭建
一.工具描述 Apache JMeter是 100 %的 java 桌面应用程序.它可以被用来测试包括基于静态和动态资源程序的性能,例如静态文件, Java Servlets , Java 对象,数 ...