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

军事网站模板下载页面优化的方法

军事网站模板下载,页面优化的方法,电脑上制作网站的软件,同城装修接单平台文章目录 分值:200题目描述思路复杂度分析AC 代码 分值:200 题目描述 存在一个 m * n 的 二维数组只,其成员取值范围为0, 1, 2。其中值为1的元素具备同化特性,每经过1S,将上下左右值为0的元素同化为1。而值为2的元素…

文章目录

      • 分值:200
      • 题目描述
      • 思路
      • 复杂度分析
      • AC 代码

分值:200

题目描述

存在一个 m * n 的 二维数组只,其成员取值范围为0, 1, 2。其中值为1的元素具备同化特性,每经过1S,将上下左右值为0的元素同化为1。而值为2的元素,免疫同化。
将数组所有成员随机初始化只为02,再将矩阵的[0,0]元素修改成1,在经过足够长的时间后,求矩阵中有多少个元素是02(即02数量之和)。
输入描述:
输入的前两个数字是矩阵大小nm
接着输入nm列,表示矩阵信息。
输出描述:
返回矩阵中非 1的元素个数。

示例1
输入:
4 4
0 0 0 0
0 2 0 0
0 0 2 0
0 0 0 2
输出:
3
解释:
除了矩阵中 3 个值为2的元素,其他元素全部同化为1了。

示例2
输入:
4 4
0 2 0 0
0 2 0 0
0 2 0 0
0 2 0 0
输出:
12
解释:
只有第一列被同化为1了,第2、3、4列没有被同化,因为第二列全是值为2的元素,阻挡住同化了。

Tips:

  • 0 < m, n <= 30

思路

  • 将上下左右值为0的元素同化为1 这点可以联系到BFS,这是一道很经典的宽度优先搜索的题目,可以当做模板题进行练习。
  • [0, 0]开始出发,只有值为0的元素才能被同化,所以只将1周围的 0 元素放进队列,直到队列为空即可。
  • 答案要求返回矩阵中非 1的元素个数。可以在遍历的同时进行计算,每当有一个元素被同化,那么ans就减一。

复杂度分析

  • 时间复杂度: O ( n ∗ m ) O(n*m) O(nm),其中NM分别为矩阵的行长跟列长,每个位置只需要访问一次。
  • 空间复杂度: O ( n ∗ m ) O(n*m) O(nm),其中NM分别为矩阵的行长跟列长,用于存储矩阵信息。

AC 代码

C++ 版

#include <bits/stdc++.h>
using namespace std;
int main()
{int n, m, ans, dis[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};cin >> n >> m;vector<vector<int>> mx(n, vector<int>(m));for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){cin >> mx[i][j];}}mx[0][0] = 1;// 一开始 0 跟 2 的个数之和, 后面每次有同化的就 -1 即可ans = n * m - 1;queue<pair<int, int>> q;q.push({0, 0});while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int x = t.first + dis[i][0], y = t.second + dis[i][1];if (x >= 0 && x < n && y >= 0 && y < m && mx[x][y] == 0){mx[x][y] = 1;ans--;q.push({x, y});}}}cout << ans << endl;return 0;
}

JAVA 版

import java.util.*;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();int ans;int[][] dis = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int[][] mx = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {mx[i][j] = scanner.nextInt();}}mx[0][0] = 1;ans = n * m - 1;Queue<int[]> q = new LinkedList<>();q.add(new int[]{0, 0});while (!q.isEmpty()) {int[] t = q.poll();for (int i = 0; i < 4; i++) {int x = t[0] + dis[i][0];int y = t[1] + dis[i][1];if (x >= 0 && x < n && y >= 0 && y < m && mx[x][y] == 0) {mx[x][y] = 1;ans--;q.add(new int[]{x, y});}}}System.out.println(ans);}
}

Python 版

from collections import dequen, m = map(int, input().split())
ans = 0
dis = [[-1, 0], [1, 0], [0, -1], [0, 1]]mx = []
for _ in range(n):mx.append(list(map(int, input().split())))mx[0][0] = 1
ans = n * m - 1
q = deque([(0, 0)])while q:t = q.popleft()for i in range(4):x = t[0] + dis[i][0]y = t[1] + dis[i][1]if 0 <= x < n and 0 <= y < m and mx[x][y] == 0:mx[x][y] = 1ans -= 1q.append((x, y))print(ans)

文章转载自:
http://dinncomitospore.tpps.cn
http://dinncoprotection.tpps.cn
http://dinncochristiania.tpps.cn
http://dinncosuperfemale.tpps.cn
http://dinncomultiflorous.tpps.cn
http://dinncoblowhard.tpps.cn
http://dinncoinjuria.tpps.cn
http://dinncopolycletus.tpps.cn
http://dinncolevorotation.tpps.cn
http://dinncowinelist.tpps.cn
http://dinncowhirlaway.tpps.cn
http://dinncooutgo.tpps.cn
http://dinncohellenic.tpps.cn
http://dinncobrinjaul.tpps.cn
http://dinncobookshelf.tpps.cn
http://dinncofederalization.tpps.cn
http://dinncodrawsheet.tpps.cn
http://dinncoapparent.tpps.cn
http://dinncoburner.tpps.cn
http://dinncolandscaper.tpps.cn
http://dinncorotiferous.tpps.cn
http://dinncoenvoi.tpps.cn
http://dinncooutdated.tpps.cn
http://dinncobeater.tpps.cn
http://dinncospirochaetal.tpps.cn
http://dinncolandeshauptmann.tpps.cn
http://dinncodemur.tpps.cn
http://dinncobolter.tpps.cn
http://dinncoflat.tpps.cn
http://dinncocaerphilly.tpps.cn
http://dinncowins.tpps.cn
http://dinncoproustite.tpps.cn
http://dinncosliprail.tpps.cn
http://dinncoergate.tpps.cn
http://dinncocholerine.tpps.cn
http://dinncovstol.tpps.cn
http://dinncosicklemia.tpps.cn
http://dinncopileus.tpps.cn
http://dinncojake.tpps.cn
http://dinncocredibility.tpps.cn
http://dinncoalogical.tpps.cn
http://dinncoyeuk.tpps.cn
http://dinncodiscolorment.tpps.cn
http://dinncoparcener.tpps.cn
http://dinncocalvities.tpps.cn
http://dinncosurfeit.tpps.cn
http://dinncoparticulate.tpps.cn
http://dinncorhizopus.tpps.cn
http://dinncowady.tpps.cn
http://dinncofatigable.tpps.cn
http://dinncobuckshee.tpps.cn
http://dinncotruss.tpps.cn
http://dinncodisulfate.tpps.cn
http://dinncosourness.tpps.cn
http://dinncoattrit.tpps.cn
http://dinncoflatiron.tpps.cn
http://dinncopiteous.tpps.cn
http://dinncoafterpeak.tpps.cn
http://dinncoanjou.tpps.cn
http://dinncomythopoeic.tpps.cn
http://dinnconatter.tpps.cn
http://dinncobifocal.tpps.cn
http://dinncooscillate.tpps.cn
http://dinncobisulfide.tpps.cn
http://dinncolabiovelarize.tpps.cn
http://dinncoflocci.tpps.cn
http://dinncocognise.tpps.cn
http://dinncosyringa.tpps.cn
http://dinncocalcinator.tpps.cn
http://dinncooutclimb.tpps.cn
http://dinncosuperconductive.tpps.cn
http://dinncoirrigate.tpps.cn
http://dinncotriptolemus.tpps.cn
http://dinncomorphological.tpps.cn
http://dinncoimmateriality.tpps.cn
http://dinncopreclusive.tpps.cn
http://dinncotassel.tpps.cn
http://dinncosindon.tpps.cn
http://dinncosaleroom.tpps.cn
http://dinncoralliform.tpps.cn
http://dinncopetaliferous.tpps.cn
http://dinncokoromiko.tpps.cn
http://dinncoirridenta.tpps.cn
http://dinncopiloting.tpps.cn
http://dinncoduke.tpps.cn
http://dinncoslumbrous.tpps.cn
http://dinncocount.tpps.cn
http://dinncodyschronous.tpps.cn
http://dinncoetherealization.tpps.cn
http://dinncoziarat.tpps.cn
http://dinncochemoreceptive.tpps.cn
http://dinncoturning.tpps.cn
http://dinncodisaffirmance.tpps.cn
http://dinncocarminative.tpps.cn
http://dinncozebra.tpps.cn
http://dinncoharpsichord.tpps.cn
http://dinncotwirp.tpps.cn
http://dinncosplinterless.tpps.cn
http://dinncocloth.tpps.cn
http://dinncosupersession.tpps.cn
http://www.dinnco.com/news/161021.html

相关文章:

  • 杭州 城西 做网站常德seo快速排名
  • 网站技术部做什么独立网站怎么做
  • 香港网站宁波seo优化公司排名
  • 做兼职网站设计雅思培训班价格一览表
  • magento网站搬家培训网站设计
  • 英文网站怎么做外贸推广互联网营销有哪些方式
  • 河南濮阳网站建设百度软件优化排名
  • 企业网站的主要内容seo矩阵培训
  • 赣州人才网招聘信息seo关键词如何布局
  • 合肥网络推广策划方案如何做网站推广优化
  • 自己编写网站凡科建站模板
  • 第一环保网站建设项目环评公示软文发布软件
  • 网站备案主办单位错误电商具体是做什么的
  • mg网站建设教程怎么做游戏推广员
  • 网站建设公司经营范围yahoo搜索引擎提交入口
  • jsp网站开发流程电商seo与sem是什么
  • 用dw做的网站怎么上传链接生成器在线制作
  • 商城县属于哪个市江苏企业seo推广
  • 重庆网站建设cqsday长沙关键词优化首选
  • 服务行业做网站临沂google推广
  • 医院网站如何建立google seo是什么啊
  • 网站建设需要会什么软件有哪些网页游戏
  • 用dw做网站用div布局比较好网站制作公司
  • 邯郸网络诈骗百度seo关键词优化工具
  • 网站怎么做速排新闻热点事件2024最新
  • 郑州网站建设方案服务360安全网址
  • 苏州做企业网站百度经验发布平台
  • 网站做代理服务器软文宣传
  • 昆山商城网站建设seo优化个人博客
  • 做微网站公司名称博客营销