Python,第192 182 180个字符
如果输入用逗号分隔,我可以保存一些。那么第一行将是d=input()
171个字符。
让我的地雷坐标从0开始而不是从1开始也会有所帮助。我花了8个字符来克服。
d=map(int,raw_input().split())
m=zip(d[2::2],d[3::2])
for y in range(d[1]):print"".join((str(sum(abs(a-x-1)|abs(b-y-1)<2for a,b in m)or'x')+'*')[(x+1,y+1)in m]for x in range(d[0]))
非高尔夫版本:
d=map(int,raw_input().split()) # Read whitespace terminated numbers into a list of numbers
xsize,ysize = d[:2] # The first two numbers are the board size
mines=zip(d[2::2],d[3::2]) # Convert items 3,4,5,6... to pairs (3,4),(5,6) representine mine coordinates
def dist(point,mine): # Distance between point (0-based coordinates) and mine (1-based coordinates)
dx = abs(mine[0]-(point[0]+1))
dy = abs(mine[1]-(point[1]+1))
return dx | dy # Should be max(dx,dy), but this is close enough. Wrong for d>=2, but returns >=2 in this case.
for y in range(ysize): # Print lines one by one
line_chars = [
(str(
sum(dist((x,y),(a,b))<2 for a,b in mines) # Number of neighboring mines
or 'x' # 'x' instead of 0
)
+'*') # For a single neighbor, we get "1*"
[(x+1,y+1)in mines] # If a mine, get the '*', else the neighbor number
for x in range(xsize)
]
print "".join(line_chars)
5 5 1
永远不会过去?