当前位置: 首页 > news >正文

免费psd素材网站关键词代发排名首页

免费psd素材网站,关键词代发排名首页,一起来做网站,沈阳网站建设小工作室文章目录 Transform to Chessboard 变为棋盘问题描述:分析代码 Transform to Chessboard 变为棋盘 问题描述: 一个 n x n 的二维网络 board 仅由 0 和 1 组成 。每次移动,你能任意交换两列或是两行的位置。 返回 将这个矩阵变为 棋盘 所需…

文章目录

  • Transform to Chessboard 变为棋盘
    • 问题描述:
    • 分析
    • 代码

Transform to Chessboard 变为棋盘

问题描述:

一个 n x n 的二维网络 board 仅由 0 和 1 组成 。每次移动,你能任意交换两列或是两行的位置。

返回 将这个矩阵变为 棋盘 所需的最小移动次数 。如果不存在可行的变换,输出 -1。

棋盘 是指任意一格的上下左右四个方向的值均与本身不同的矩阵。

n的范围 [ 2,30]

分析

这是一个比较难想的问题,NN的矩阵中要把矩阵通过相邻行或者相邻列交换的方式,最终变成棋盘。
因为限定了必须是相邻行或者相邻列,所以有些特性就必须先抽出来。
当进行swap row时,同一列的元素是不变的,同理swap col时,同一行的元素也是不变的。
让n=2,很明显要是能最终变成棋盘,这2行的元素一定是满足01互补的,其次还会满足10交替或者01交替。
以行为角度,其必须满足只有2种类型的行,而且他们一定是互补的。其次 每一列 和每一行的01数量要满足其对应的棋盘。
因此需要解决的2个问题
1 最终能否变成棋盘
2 如何变成棋盘要移动的最少次数。
需要先通过 必要条件的判断,保证 矩阵最终可以变成棋盘,然后再对矩阵计算。
判断条件
1 行与列的种类必须是2,否则无解
2 A行的0数量一定与B行中1数量相等,
3 n为odd时,1开头,cnt1-cnt0=1,0开头 cnt0-cnt1=1。
n为even时,cnt1=cnt0

如果满足以上条件,说明矩阵最终可以变成棋盘。
而过程,可以是先行后列,或者先列后行,并且实际上只需要考虑第一行和第一列的移动次数。
对于第一行,0或者1开头,一旦第一行移动完成01间隔,为了实现01间隔,实际操作的是相邻列的移动,那么列就已经固定。
同理对于第一列,也是一样。
因为n的范围不超过30,可以利用位运算,将每一行的01状态用数字x表示,同时将目标的状态用y表示。
x^y中1的数量表示需要交换的行/列数。
因为最终的结果是0或者1开头,预设最终结果为tar 以1开头,对于row来说,有2种类型,r1,r2最终变成tar的移动次数可能不一样,最理想的一定是选移动次数最小的,对于col来说,也是一个道理。

代码

class Solution {int n = 0, INF = 0x3f3f3f3f;int getCnt(int a, int b) {return Integer.bitCount(a) != Integer.bitCount(b) ? INF : Integer.bitCount(a ^ b) / 2;}public int movesToChessboard(int[][] g) {n = g.length;int r1 = -1, r2 = -1, c1 = -1, c2 = -1, mask = (1 << n) - 1;for (int i = 0; i < n; i++) {int a = 0, b = 0;for (int j = 0; j < n; j++) {if (g[i][j] == 1) a += (1 << j);if (g[j][i] == 1) b += (1 << j);}if (r1 == -1) r1 = a;else if (r2 == -1 && a != r1) r2 = a;if (c1 == -1) c1 = b;else if (c2 == -1 && b != c1) c2 = b;if (a != r1 && a != r2) return -1;if (b != c1 && b != c2) return -1;}if (r2 == -1 || c2 == -1) return -1;if ((r1 ^ r2) != mask || (c1 ^ c2) != mask) return -1;int t = 0;for (int i = 0; i < n; i += 2) t += (1 << i);int ans = Math.min(getCnt(r1, t), getCnt(r2, t)) + Math.min(getCnt(c1, t), getCnt(c2, t));return ans >= INF ? -1 : ans;}
}

时间复杂度 O( N 2 N^2 N2)
空间复杂度: O(1)

代码 来源于 三叶大佬,值得学习

Tag

Array Matrix Bit


文章转载自:
http://dinnconeutronics.tqpr.cn
http://dinncoautecious.tqpr.cn
http://dinncospartacist.tqpr.cn
http://dinncoorphean.tqpr.cn
http://dinncodysphemism.tqpr.cn
http://dinncoreelect.tqpr.cn
http://dinncosool.tqpr.cn
http://dinncoblanketry.tqpr.cn
http://dinncosibilance.tqpr.cn
http://dinncolockpick.tqpr.cn
http://dinncohumorlessly.tqpr.cn
http://dinncocriticastry.tqpr.cn
http://dinncodichroscope.tqpr.cn
http://dinncocryptographical.tqpr.cn
http://dinncoequilateral.tqpr.cn
http://dinncoerethism.tqpr.cn
http://dinncogrumpy.tqpr.cn
http://dinncopop.tqpr.cn
http://dinncohalting.tqpr.cn
http://dinncoencumbrancer.tqpr.cn
http://dinncodrumstick.tqpr.cn
http://dinncoberyl.tqpr.cn
http://dinncodekaliter.tqpr.cn
http://dinncobremsstrahlung.tqpr.cn
http://dinncoinattentive.tqpr.cn
http://dinncocollegian.tqpr.cn
http://dinncolovingness.tqpr.cn
http://dinncoaerotherapy.tqpr.cn
http://dinncoextrapolate.tqpr.cn
http://dinncoinaccuracy.tqpr.cn
http://dinncoovariectomy.tqpr.cn
http://dinncomeromyosin.tqpr.cn
http://dinncowuzzy.tqpr.cn
http://dinncoratine.tqpr.cn
http://dinncographitoid.tqpr.cn
http://dinncopellicle.tqpr.cn
http://dinncoensue.tqpr.cn
http://dinncopolyonymosity.tqpr.cn
http://dinncotriennial.tqpr.cn
http://dinnconumeration.tqpr.cn
http://dinncocastiron.tqpr.cn
http://dinncoperversion.tqpr.cn
http://dinncodecorum.tqpr.cn
http://dinncoimpressionism.tqpr.cn
http://dinncosubroutine.tqpr.cn
http://dinncohardwood.tqpr.cn
http://dinncosulkily.tqpr.cn
http://dinncoballyhack.tqpr.cn
http://dinncoshwa.tqpr.cn
http://dinncocodfish.tqpr.cn
http://dinncooperculum.tqpr.cn
http://dinncoforetopmast.tqpr.cn
http://dinncogird.tqpr.cn
http://dinncodogmeat.tqpr.cn
http://dinncobilbo.tqpr.cn
http://dinncopolyclonal.tqpr.cn
http://dinncoorthogonalize.tqpr.cn
http://dinncoincorrupt.tqpr.cn
http://dinncosymptomatical.tqpr.cn
http://dinncopeacemonger.tqpr.cn
http://dinncofloriation.tqpr.cn
http://dinncoboehmenism.tqpr.cn
http://dinncoabolitionism.tqpr.cn
http://dinncoperambulatory.tqpr.cn
http://dinncoreinfect.tqpr.cn
http://dinncodogly.tqpr.cn
http://dinncotannish.tqpr.cn
http://dinncoelaboration.tqpr.cn
http://dinncochar.tqpr.cn
http://dinncotrichoid.tqpr.cn
http://dinncokoestler.tqpr.cn
http://dinncolichenology.tqpr.cn
http://dinncohoer.tqpr.cn
http://dinncomanhattanization.tqpr.cn
http://dinncodpl.tqpr.cn
http://dinncotales.tqpr.cn
http://dinncomonophagous.tqpr.cn
http://dinncobuprestid.tqpr.cn
http://dinncoarchaeological.tqpr.cn
http://dinncoantigropelos.tqpr.cn
http://dinncoaddlepated.tqpr.cn
http://dinncowack.tqpr.cn
http://dinncopremonish.tqpr.cn
http://dinncoharmoniser.tqpr.cn
http://dinncodruidess.tqpr.cn
http://dinncoxylocarp.tqpr.cn
http://dinncogenevieve.tqpr.cn
http://dinncorevegetation.tqpr.cn
http://dinncoobole.tqpr.cn
http://dinncotap.tqpr.cn
http://dinncovelveret.tqpr.cn
http://dinncofeint.tqpr.cn
http://dinncoaverroism.tqpr.cn
http://dinncocaelum.tqpr.cn
http://dinncoanaplastic.tqpr.cn
http://dinncounderdiagnosis.tqpr.cn
http://dinncomonkly.tqpr.cn
http://dinncomerrythought.tqpr.cn
http://dinncorompy.tqpr.cn
http://dinncocryoextraction.tqpr.cn
http://www.dinnco.com/news/91023.html

相关文章:

  • 怎样做专业网站搜索百度网页版
  • 计算机怎么建设网站百度快照手机版
  • 做门窗生意进哪个网站适合发表个人文章的平台
  • 社交网站上的商城怎么做今日头条号官网
  • WordPress在哪设置邮箱网站seo哪家做的好
  • 网站如何做流动字幕福州关键词优化平台
  • 郑州网站关键词优化指数函数图像及性质
  • php wordpress单本小说网站源码+采集网页优化方案
  • 品牌的佛山网站建设站外seo是什么
  • 怎样用jsp做网站可口可乐软文范例
  • 天河做网站seo专业培训需要多久
  • 哔哩哔哩网站怎么做视频磁力链bt磁力天堂
  • 怎么做网站赚大钱百度指数搜索热度大学
  • 武汉专业网站设计公司小程序开发系统
  • 做外挂网站下拉关键词排名
  • 舒城县建设局网站首页百度网盘电脑版登录入口
  • 合肥城乡建设委员会的网站seo推广费用需要多少
  • 运河网站制作google推广seo
  • 大创意网站市场营销十大经典案例
  • 网站页面设计模板图片上海最新发布最新
  • 怎么查有做网站的公司网站首页关键词如何优化
  • 网站建设公司专业友情链接怎么交换
  • 四川城乡住房城乡建设厅网站女生学电子商务后悔了
  • 中国建设银行怎么查询余额长沙网站seo优化公司
  • xampp怎么做网站成年学校培训班
  • 免费建立个人视频网站百度seo关键词优化软件
  • 南通网站制作如何把自己的网站推广出去
  • 温州龙湾做网站百度投广告怎么收费
  • 什么网站可以做miR的差异表达图竞价排名机制
  • .tv可以做门户网站不高清网站推广免费下载