题目描述
Minesweeper Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can't remember. The goal of the game is to find where all the mines are located within a M x N field. The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*'' character. If we represent the same field by the hint numbers described above, we end up with the field on the right: *... .... .*.. .... *100 2210 1*10 1110
输入格式
The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m$ \le$100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field. Safe squares are denoted by ``.'' and mine squares by ``*,'' both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.
输出格式
For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.
样例输入
复制
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
样例输出
复制
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
方法一
解题思路:
给原矩阵中增加一个padding即可避免判断“需要找几个邻居”的问题(灵感来源于计算机视觉中的Padding操作)
counter=1
while True:
m,n=map(int,input().split())
if m==n==0:
break
# padding=['.' for _ in range(n+2)]
padding=['.']*(n+2)
# field,mine=[padding,padding],[[0 for _ in range(n+1)] for _ in range(m+1)]
field,mine=[padding,padding],[[0]*(n+1) for _ in range(m+1)]
for i in range(1,m+1):
field.insert(i,['.']+list(input())+['.'])
for i in range(1,m+1):
for j in range(1,n+1):
if field[i][j]=='*':
mine[i][j]='*'
else:
temp=[field[i+1][j+1],field[i-1][j-1],field[i+1][j-1],field[i-1][j+1],
field[i][j+1],field[i][j-1],field[i+1][j],field[i-1][j]]
mine[i][j]=sum(1 for k in temp if k=='*')
print(f"Field #{counter}:")
counter+=1
for i in range(1,m+1):
print(*mine[i][1:],sep='')
print()
方法二
解题思路:
就是选把点换成数字0,判断某个元素是否是雷,如果是雷就将它周围不是雷的元素+1
cot = 1
while 1:
row, col = map(int, input().split())
if row == col == 0:
break
orgl = []
for i in range(row):
orgl.append(list(input()))
retl = [[0] * col for x in range(row)]
for i in range(row):
for j in range(col):
if orgl[i][j] == '*':
retl[i][j] = '*'
for i in range(row):
for j in range(col):
if orgl[i][j] == '*':
if i - 1 >= 0 and j - 1 >= 0 and orgl[i - 1][j - 1] != '*':
retl[i - 1][j - 1] += 1
if i - 1 >= 0 and j + 1 <= col - 1 and orgl[i - 1][j + 1] != '*':
retl[i - 1][j + 1] += 1
if i + 1 <= row - 1 and j + 1 <= col - 1 and orgl[i + 1][j + 1] != '*':
retl[i + 1][j + 1] += 1
if i + 1 <= row - 1 and j - 1 >= 0 and orgl[i + 1][j - 1] != '*':
retl[i + 1][j - 1] += 1
if i + 1 <= row - 1 and orgl[i + 1][j] != '*':
retl[i + 1][j] += 1
if i - 1 >= 0 and orgl[i - 1][j] != '*':
retl[i - 1][j] += 1
if j + 1 <= col - 1 and orgl[i][j + 1] != '*':
retl[i][j + 1] += 1
if j - 1 >= 0 and orgl[i][j - 1] != '*':
retl[i][j - 1] += 1
print(f"Field #{cot}:")
for i in range(row):
print(*retl[i][:], sep='')
cot += 1
print()