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) + ...
随机推荐
- Flask的WTforms
一.简单介绍 WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 类似于Django中的modelform 安装: pip3 install wtforms 二.简 ...
- 服务器 apache配置https,http强制跳转https(搭建http与https共存)
公司linux服务器上的nginx的已经改成https了,现在还剩下一个windows云服务器没配置. 环境 windows wampserver2.5 64位 1.腾讯云申请的ssl 包含三个文件: ...
- 程序猿的日常——Java基础之equals与hashCode
equals和hashCode是我们日常开发最常使用的方法,但是因为一般都使用默认的规则,因此也很少会引起关注.不过了解他们的用途和设计的原则,还是会帮助我们更好的设计代码. equals equal ...
- Android安全防护防护———Android 端常见的安全问题
Android安全防护防护——加密算法:传送门https://www.cnblogs.com/huangjialin/p/9694488.html 组件安全 activity劫持 简单来说就是正常的a ...
- 怎样使用Navicat Premium导出导入mysql数据库
首先,在Navicat Premium中连接要导出数据库的mysql数据库. 2 1.填写好连接数据库的信息后就可以连接到需要导出的数据库了. 3 打开要导出的数据库. 4 将数据库的结构和数据导出为 ...
- 模仿 AppStore 顶部动画
App Store 顶部动画 App Store 中 Games.Apps.Updates 的顶部动画的特点: 自然状态下是大标题,右边有一个 button 顶上去时,变成小标题,右边按钮消失 导航栏 ...
- NOIP上机测试注意事项
由于近期模拟题原地**次数较多,故写一篇警示文 1,头文件 1.1正式比赛中,反正我不敢用bits/stdc++.h. 1.2正式比赛中,建议打出以下十个库 #include<iostream& ...
- C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇)
C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇) 名词解释:apxs apxs is a tool for building and installing ext ...
- 隐型马尔科夫模型(HMM) 简介
先介绍一下马尔科夫模型: 马尔可夫模型(Markov Model)是一种统计模型,广泛应用在语音识别,词性自动标注,音字转换,概率文法等各个自然语言处理等应用领域.经过长期发展,尤其是在语音识别中的成 ...
- DotNetOpenAuth 使用指南
这几天一直在研究DotNetOpenAuth,源码处处是坑啊!写此文只为大家更顺利掌握DotNetOpenAuth使用方法,尽量少走弯路. 说明一下:我的环境是Win7 64 VS2015 upd ...