【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
题目如下:
Given a
m x nbinary matrixmat. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.Return the minimum number of steps required to convert
matto a zero matrix or -1 if you cannot.Binary matrix is a matrix with all cells equal to 0 or 1 only.
Zero matrix is a matrix with all cells equal to 0.
Example 1:
Input: mat = [[0,0],[0,1]]
Output: 3
Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.Example 2:
Input: mat = [[0]]
Output: 0
Explanation: Given matrix is a zero matrix. We don't need to change it.Example 3:
Input: mat = [[1,1,1],[1,0,1],[0,0,0]]
Output: 6Example 4:
Input: mat = [[1,0,0],[1,0,0]]
Output: -1
Explanation: Given matrix can't be a zero matrixConstraints:
m == mat.lengthn == mat[0].length1 <= m <= 31 <= n <= 3mat[i][j]is 0 or 1.
解题思路:最大就是3*3的矩阵,BFS把每种情况都试一遍就好了。
代码如下:
class Solution(object):
def minFlips(self, mat):
"""
:type mat: List[List[int]]
:rtype: int
"""
import copy
def isAllZero(grid):
count = 0
for i in grid:
count += sum(i)
return count == 0 def encode(grid):
grid_s = ''
for i in range(len(grid)):
for j in range(len(grid[i])):
grid_s += str(grid[i][j])
if i != len(grid) - 1 : grid_s += '#'
return grid_s
def decode(grid_s):
gl = grid_s.split('#')
grid = []
for i in gl:
tl = []
for j in list(i):
tl.append(int(j))
grid.append(tl)
return grid res = float('inf')
queue = [(encode(mat),0)]
dic = {}
dic[encode(mat)] = 0 while len(queue) > 0:
gs,step = queue.pop(0)
grid = decode(gs)
if isAllZero(grid):
res = min(res,step)
continue
elif res <= step:
continue
for i in range(len(grid)):
for j in range(len(grid[i])):
#if grid[i][j] == 0: continue
new_grid = copy.deepcopy(grid)
if new_grid[i][j] == 1:
new_grid[i][j] = 0
else:new_grid[i][j] = 1
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for (x, y) in directions:
if x + i >= 0 and x + i < len(grid) and y + j >= 0 and y + j < len(grid[0]):
if new_grid[x + i][y + j] == 0:
new_grid[x + i][y + j] = 1
else:
new_grid[x + i][y + j] = 0
encode_Str = encode(new_grid)
if encode_Str not in dic or dic[encode_Str] > step + 1:
queue.append((encode(new_grid), step + 1))
dic[encode_Str] = step + 1
return res if res != float('inf') else -1
【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix的更多相关文章
- LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (最少翻转次数将二进制矩阵全部置为0)
给一个矩阵mat,每个格子都是0或1,翻转一个格子会将该格子以及相邻的格子(有共同边)全部翻转(0变为1,1变为0) 求问最少需要翻转几次将所有格子全部置为0. 这题的重点是数据范围,比赛结束看了眼数 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】995. Minimum Number of K Consecutive Bit Flips
题目如下: In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) suba ...
- 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- 数据排序 sort
排序命令: 常和管道进行协作的命令 -sort (默认使用字符的第一个字符进行排序) -n 按数字排序 -r 反序排序 -o 结果 输出到文件 -t 分隔符 (sort -n -t &qu ...
- OVS+Docker
两台机器操作一样就是IP不同但是设置都是相同的: A机器:192.168.71.142 docker0:172.17.42.1 B机器:192.168.71.136 docker0:172.17.43 ...
- Web项目测试流程总结
个人知识脑图总结 - 未完全(工作项目脑图总结存于网盘中)
- 怎样使用构造函数: Vue()?
1. 新建一个 .html 文件 => 引入一个在线的 vue 库 => 写一个带 id 的 html 标签 => 写一个 script 标签, 这里的 vApp 是 Vue() 这 ...
- C#颜色对话框(WPF可用)
System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog(); //允许使用该对话框的自定 ...
- JS基础_if注意问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- postman传数组参数,二维数组,多维数组
一维数组: 传递: 接收: 二维数组: 传递: 接收: 依此类推,
- java后台读取配置文件
前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家: 先附上代码吧: package com.shafei.util; import java.io. ...
- python 访问权限
访问权限 权限: 公有的:类中的普通属性和方法,默认都是公有的,可以在类的内部.外部.子类中使用 私有的:定义是在前面加两个'_',只能在本类的内部使用,不能再外部及子类中使用 示例: class P ...
- XSS防御和绕过1
原理:对用户输入没做过滤和处理,是用户可以输入一些东西(例如js),控制输出达到一些攻击目的 1.DOM型 基于DOM的XSS有时也称为type0XSS.当用户能够通过交互修改浏览器页面中的DOM(D ...
