classMine:def__init__(self, row, col):
self.row, self.col = row, col # 地图的行列# 搜索的范围
self.neighbors =[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
self.grids =[[0for j inrange(col +1)]for i inrange(row +1)]# 地图# 在地图中标出地雷defset_mines(self, coordinates):for coordinate in coordinates:
self.grids[coordinate[0]][coordinate[1]]=-1# 检测地雷defhunt(self):for i inrange(1, self.row +1):for j inrange(1, self.col +1):if self.grids[i][j]==-1:for neighbor in self.neighbors:
r, c = i + neighbor[0], j + neighbor[1]ifnot(1<= r <= self.row and1<= c <= self.col):continueif self.grids[r][c]==-1:continue
self.grids[r][c]+=1# 输出def__str__(self):
res =""for i inrange(1, self.row +1):for j inrange(1, self.col +1):if self.grids[i][j]>=0:
res +=" "
res +=str(self.grids[i][j])
res +=" "
res +="\n"return res
if __name__ =="__main__":
I =[int(i)for i ininput().split()]
J =[int(i)for i ininput().split()](M, N)=[int(i)for i ininput("the number of rows and columns on the grid = ").split()]
mine = Mine(row=M, col=N)
mine.set_mines(coordinates=zip(I, J))
mine.hunt()print(mine)
Problem 65. Word Counting and Indexing
defsolve(str_list):
word_table =sorted(list(set((' '.join(str_list)).split())))
str2index ={}
index_list =[]for i inrange(len(word_table)):
str2index[word_table[i]]= i +1for nums in str_list:
index_list.append([])for num in nums.split():
index_list[-1].append(str2index[num])return word_table, index_list,if __name__ =="__main__":
str_list ="one two three","two one four zero"
word_table, index_list = solve(str_list)print(word_table)print(index_list)#word_table = {'four','one','three','two','zero'}#str_index_list = {[2 4 3],[4 2 1 5]}.