本文主要是介绍python 炸敌人。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给定一个二维矩阵,每一个格子可能是一堵墙 W、一个敌人E或者空0(数字0)。返回用一个炸弹可杀死的最多敌人数。炸弹会杀死所有在同一行和同一列没有墙阻隔的敌人墙不会被摧毁,只能在空地放置炸弹。
class Solution:def maxKilledEnemies(self, grid):rows = len(grid)cows = len(grid[0])cnt = 0for i in range(rows):for j in range(cows):if grid[i][j] == '0':# 分析所在行合理范围有几个E,j在变化s = 0n = j - 1while n >= 0:if grid[i][n] == 'E':s += 1elif grid[i][n] == 'W':breakn -= 1n = j + 1while n < cows:if grid[i][n] == 'E':s += 1elif grid[i][n] == 'W':breakn += 1# 分析所在列合理范围有几个E, i在变化n = i - 1while n >= 0:if grid[n][j] == 'E':s += 1elif grid[n][j] == 'W':breakn -= 1n = i + 1while n < rows:if grid[n][j] == 'E':s += 1elif grid[n][j] == 'W':breakn += 1cnt = max(cnt, s)return cntif __name__ == '__main__':g = ["0E00","E0WE","0E00"]solution = Solution()print(solution.maxKilledEnemies(g))
这篇关于python 炸敌人。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!