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. Educational Codeforces Round 25 B. Five-In-a-Row

    题目链接:http://codeforces.com/contest/825/problem/B B. Five-In-a-Row time limit per test 1 second memor ...

  2. Zookeeper数据存储总结

    Zookeeper快照文件和事物操作文件以文件的形式存储在硬盘上,以快照文件为主,日志文件为辅.因为当对内存数据进行变更的时候,会保证将事务操作记入log日志,而snapshot只是内存某一个时刻影像 ...

  3. Java并发编程之happens-before

    happens-before是JMM最核心的概念,理解happens-before是理解JMM的关键. 一.JMM的设计 首先,让我们先分析一下JMM的设计意图.从JMM的设计者的角度,在设计JMM的 ...

  4. python相见恨晚的库

    1)基本工具: virtualenv(虚拟环境)pip.setuptools (e.g. easy_install,这些东西肯定要呢)ipython(用了以后,就不再想用普通的python shell ...

  5. iOS-消除CocoaPods内容警告

    前言 2018年2月26日 农历正月十一 星期一 今天就想更新下博客,内容不多,心情复杂: 突然想吟诗一首: 其实,我是一个善良的人: 其实,我是一枚... 算了,还是说正事吧 消除CocoaPods ...

  6. 调用百度地图开发平台的JavascriptAPI实现将市县位置转换成坐标

    最近的项目要做的地图比较多,有的还比较复杂,而地图用到的坐标,上网找json文件更是良莠不齐的.真是让人伤脑筋,后来突然想到了百度地图开发平台,没想到真的有对应的API哦,谢天谢地!!!下面说一下完整 ...

  7. leetcode-908-最小差值 I

    题目描述: 给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回  ...

  8. 【GDOI2015】 推箱子 状态压缩+bfs

    请注意$8$是一个美妙的数字 考虑到$8\times 8=64$,而一个unsigned long long是$64$位的,所以考虑用一个$01$状态存储箱子.考虑到箱子能转动,那么四种情况都存一下就 ...

  9. POJ 2247

    #include<iostream> #include<algorithm> #include<vector> #include<string> #in ...

  10. 剑指offer三十三之丑数

    一.题目 如果一个数的因子中,出去1和本身以外,质数因子只包含2.3和5,则把改数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质数因子7. 习惯上我们把1当做是第一个 ...