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

织梦高端html5网站建设工作室网络公司网站模板台州网站建设优化

织梦高端html5网站建设工作室网络公司网站模板,台州网站建设优化,如何做视频解析网站,wordpress广告位插件目录 卡玛网 101.孤岛的总面积 卡玛网 102.沉没孤岛 卡玛网 103. 水流问题 卡玛网 104.建造最大岛屿 卡玛网 101.孤岛的总面积 题目 101. 孤岛的总面积 思路 代码随想录:101.孤岛的总面积 重点: 首先遍历图的四条边,把其中的陆地及…

目录

卡玛网 101.孤岛的总面积

卡玛网 102.沉没孤岛

卡玛网 103. 水流问题

卡玛网 104.建造最大岛屿

卡玛网 101.孤岛的总面积

题目

101. 孤岛的总面积

思路

代码随想录:101.孤岛的总面积

重点: 首先遍历图的四条边,把其中的陆地及其位于的岛屿都记录为 0,然后再遍历整张地图,并开始计算面积。

题解

DFS:

import java.util.*;public class Main {private static final int[][] DIR = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][] graph = new int[N][M];for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {graph[i][j] = sc.nextInt();}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if ((i == 0 || j == 0 || i == N - 1 || j == M - 1) && graph[i][j] == 1)dfs(graph, i, j, N, M);}}int totalArea = 0;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 1)totalArea += dfs(graph, i, j, N, M);}}System.out.println(totalArea);}private static int dfs(int[][] graph, int x, int y, int N, int M) {if (x < 0 || y < 0 || x >= N || y >= M || graph[x][y] != 1)return 0;graph[x][y] = 0;int area = 1;for (int i = 0; i < 4; i++) {int nextX = x + DIR[i][0];int nextY = y + DIR[i][1];area += dfs(graph, nextX, nextY, N, M);}return area;}
}

BFS:

import java.util.*;public class Main {private static final int[][] DIR = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][] graph = new int[N][M];for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {graph[i][j] = sc.nextInt();}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if ((i == 0 || j == 0 || i == N - 1 || j == M - 1) && graph[i][j] == 1)bfs(graph, i, j, N, M);}}int totalArea = 0;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 1)totalArea += bfs(graph, i, j, N, M);}}System.out.println(totalArea);}private static int bfs(int[][] graph, int x, int y, int N, int M) {Deque<int[]> deque = new LinkedList<>();deque.offer(new int[]{x, y});graph[x][y] = 0;int area = 1;while (!deque.isEmpty()) {int[] cur = deque.poll();int curX = cur[0];int curY = cur[1];for (int i = 0; i < 4; i++) {int nextX = curX + DIR[i][0];int nextY = curY + DIR[i][1];if (nextX < 0 || nextY < 0 || nextX >= N || nextY >= M || graph[nextX][nextY] != 1)continue;graph[nextX][nextY] = 0;area++;deque.offer(new int[]{nextX, nextY});}}return area;}
}

卡玛网 102.沉没孤岛

题目

102. 沉没孤岛

思路

代码随想录:102.沉没孤岛

img

题解

DFS:

import java.util.*;public class Main {private static final int[][] DIR = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][] graph = new int[N][M];for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {graph[i][j] = sc.nextInt();}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if ((i == 0 || j == 0 || i == N - 1 || j == M - 1) && graph[i][j] == 1)dfs(graph, i, j, N, M);}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 2) {System.out.print(1 + " ");} else {System.out.print(0 + " ");}}System.out.println();}}private static void dfs(int[][] graph, int x, int y, int N, int M) {if (x < 0 || y < 0 || x >= N || y >= M || graph[x][y] != 1)return;graph[x][y] = 2;for (int i = 0; i < 4; i++) {int nextX = x + DIR[i][0];int nextY = y + DIR[i][1];dfs(graph, nextX, nextY, N, M);}}
}

BFS:

import java.util.*;public class Main {private static final int[][] DIR = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][] graph = new int[N][M];for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {graph[i][j] = sc.nextInt();}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if ((i == 0 || j == 0 || i == N - 1 || j == M - 1) && graph[i][j] == 1)bfs(graph, i, j, N, M);}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 2) {System.out.print(1 + " ");} else {System.out.print(0 + " ");}}System.out.println();}}private static void bfs(int[][] graph, int x, int y, int N, int M) {Deque<int[]> deque = new LinkedList<>();deque.offer(new int[]{x, y});graph[x][y] = 2;while (!deque.isEmpty()) {int[] cur = deque.poll();int curX = cur[0];int curY = cur[1];for (int i = 0; i < 4; i++) {int nextX = curX + DIR[i][0];int nextY = curY + DIR[i][1];if (nextX < 0 || nextY < 0 || nextX >= N || nextY >= M || graph[nextX][nextY] != 1)continue;graph[nextX][nextY] = 2;deque.offer(new int[]{nextX, nextY});}}}
}

卡玛网 103. 水流问题

题目

103. 水流问题

思路

代码随想录:103.水流问题

初步思路是遍历图中的所有点,判断是否能同时到达第一边界和第二边界,时间复杂度为 O(N2 * M2),超时。

优化方案:逆向思维,分别从第一边界和第二边界逆流而上,将能到达的节点进行标记,最后两边都标记了的节点就是目标节点。

img img

题解

DFS:

import java.util.*;public class Main {private static final int[][] DIR = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][] graph = new int[N][M];for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {graph[i][j] = sc.nextInt();}}boolean[][] firstVisited = new boolean[N][M];boolean[][] secondVisited = new boolean[N][M];for (int i = 0; i < N; i++) {dfs(graph, i, 0, firstVisited);//从左边界开始dfs(graph, i, M - 1, secondVisited);//从右边界开始}for (int j = 0; j < M; j++) {dfs(graph, 0, j, firstVisited);//从上边界开始dfs(graph, N - 1, j, secondVisited);//从下边界开始}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (firstVisited[i][j] && secondVisited[i][j]) {System.out.print(i + " ");System.out.println(j);}}}}private static void dfs(int[][] graph, int x, int y, boolean[][] visited) {if (x < 0 || y < 0 || x >= graph.length || y >= graph[0].length || visited[x][y])return;visited[x][y] = true;for (int i = 0; i < 4; i++) {int nextX = x + DIR[i][0];int nextY = y + DIR[i][1];if (nextX < 0 || nextY < 0 || nextX >= graph.length || nextY >= graph[0].length || visited[nextX][nextY] || graph[nextX][nextY] < graph[x][y])continue;dfs(graph, nextX, nextY, visited);}}
}

卡玛网 104.建造最大岛屿

题目

104. 建造最大岛屿

思路

代码随想录:104.建造最大岛屿

  1. 第一次遍历地图,得出各个岛屿的面积,并做独立的编号,记录在HashMap中,key 为岛屿编号,value 为岛屿面积。
  2. 第二次遍历地图,遍历为 0 的方格,将其变为 1,并统计该方格周边的岛屿面积,将其相邻面积加在一起,得到新建的岛屿面积。
  3. 遍历所有 0 之后,可以得到最大面积。
img img img

题解

import java.util.*;public class Main {// 定义四个方向static int[][] DIR = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};static int mark = 2;  // 标记岛屿编号,初始值为2(0代表水,1代表未标记的岛屿)public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][] grid = new int[N][M];for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {grid[i][j] = sc.nextInt();}}//记录岛屿编号以及面积HashMap<Integer, Integer> islandSizeMap = new HashMap<>();//判断是否全为陆地boolean isAllIsland = true;// 标记每片岛屿并记录面积for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (grid[i][j] == 0) {isAllIsland = false;} else if (grid[i][j] == 1) {int currentArea = dfs(grid, i, j);islandSizeMap.put(mark, currentArea);mark++;}}}//如果全为陆地,直接返回总面积int result = isAllIsland ? N * M : 0;// 遍历每个水格子,计算可能的最大岛屿面积for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (grid[i][j] == 0) {HashSet<Integer> set = new HashSet<>();int newArea = 1;// 计算周围的不同岛屿面积之和for (int[] dir : DIR) {int newX = i + dir[0];int newY = j + dir[1];if (newX < 0 || newX >= N || newY < 0 || newY >= M) {continue;}int currentMark = grid[newX][newY];if (currentMark > 1 && !set.contains(currentMark)) {set.add(currentMark);newArea += islandSizeMap.get(currentMark);}}result = Math.max(result, newArea);}}}System.out.println(result);}// DFSprivate static int dfs(int[][] grid, int x, int y) {if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != 1) {return 0;}grid[x][y] = mark;int area = 1;for (int[] dir : DIR) {area += dfs(grid, x + dir[0], y + dir[1]);}return area;}
}

文章转载自:
http://planogamete.c7510.cn
http://inflatable.c7510.cn
http://reciprocate.c7510.cn
http://leatherleaf.c7510.cn
http://crypto.c7510.cn
http://eton.c7510.cn
http://rarp.c7510.cn
http://outroad.c7510.cn
http://miyazaki.c7510.cn
http://unset.c7510.cn
http://incoordinately.c7510.cn
http://pollyanna.c7510.cn
http://biunique.c7510.cn
http://sitfast.c7510.cn
http://voltammetry.c7510.cn
http://homostasis.c7510.cn
http://hvar.c7510.cn
http://acetylsalicylate.c7510.cn
http://shahaptin.c7510.cn
http://swordproof.c7510.cn
http://bedeswoman.c7510.cn
http://camorrism.c7510.cn
http://martini.c7510.cn
http://akyab.c7510.cn
http://egret.c7510.cn
http://interclavicular.c7510.cn
http://safflower.c7510.cn
http://sublunate.c7510.cn
http://epigram.c7510.cn
http://illude.c7510.cn
http://waec.c7510.cn
http://gametocyte.c7510.cn
http://remotion.c7510.cn
http://quadplex.c7510.cn
http://turcocentric.c7510.cn
http://digitoplantar.c7510.cn
http://fermentable.c7510.cn
http://perlis.c7510.cn
http://radioprotective.c7510.cn
http://strategos.c7510.cn
http://parasynapsis.c7510.cn
http://nathless.c7510.cn
http://gascounter.c7510.cn
http://unequable.c7510.cn
http://ammine.c7510.cn
http://thermonasty.c7510.cn
http://sciurine.c7510.cn
http://among.c7510.cn
http://pliant.c7510.cn
http://resell.c7510.cn
http://lucia.c7510.cn
http://polysynthetism.c7510.cn
http://haphazard.c7510.cn
http://saltwater.c7510.cn
http://fley.c7510.cn
http://blossom.c7510.cn
http://daddle.c7510.cn
http://hyperborean.c7510.cn
http://arminian.c7510.cn
http://ancon.c7510.cn
http://phosphorograph.c7510.cn
http://sinistral.c7510.cn
http://microbus.c7510.cn
http://momento.c7510.cn
http://trichogenous.c7510.cn
http://redoubted.c7510.cn
http://infrahuman.c7510.cn
http://flounce.c7510.cn
http://lunes.c7510.cn
http://unpitying.c7510.cn
http://melanoma.c7510.cn
http://bistort.c7510.cn
http://batch.c7510.cn
http://logorrhea.c7510.cn
http://gradeability.c7510.cn
http://hallstattian.c7510.cn
http://privet.c7510.cn
http://elegiast.c7510.cn
http://glossotomy.c7510.cn
http://elastomer.c7510.cn
http://martemper.c7510.cn
http://trustless.c7510.cn
http://pterosaurian.c7510.cn
http://peel.c7510.cn
http://devitrification.c7510.cn
http://despotically.c7510.cn
http://basophilous.c7510.cn
http://tartaric.c7510.cn
http://conservator.c7510.cn
http://periodate.c7510.cn
http://workless.c7510.cn
http://changeling.c7510.cn
http://soldan.c7510.cn
http://entomophagous.c7510.cn
http://osteosis.c7510.cn
http://betwixt.c7510.cn
http://jukes.c7510.cn
http://pussycat.c7510.cn
http://copasetic.c7510.cn
http://tempersome.c7510.cn
http://www.zhongyajixie.com/news/78966.html

相关文章:

  • 做网站创业国内广告投放平台
  • 合肥制作网站的公司简介软文营销经典案例200字
  • win10做网站服务器南昌seo实用技巧
  • 国家工信部网站备案必应搜索引擎
  • 做网课网站seo关键词选取工具
  • 做爰全过程免费的视网站频百度网盘怎么用
  • 网站正在建设中亚洲以网红引流促业态提升
  • 为什么做网站要服务器 和域名海淀网站建设公司
  • 如何做企业网站推广网络营销考试答案
  • 成都网站开发公司有哪些seo模拟点击算法
  • 成品网站货源入口百度百度推广
  • 购物网站开发一般使用什么语言绍兴seo管理
  • 公司名称变更整站优化系统厂家
  • 买服务器做网站主机百度seo关键词优化推荐
  • 手机版网站如何做图片滚动友情链接如何添加
  • 建一个全部由自己控制的网站需要多少钱百度推广官网网站
  • 商业网站是怎么做的企业营销策划方案
  • 江苏网站seo平台重庆高端网站seo
  • 网站欢迎页源码什么是软文
  • 怎么做网站营销个人如何做百度推广
  • 广东网站开发哪家抖音搜索关键词排名
  • 包装网站建设价格楚雄今日头条新闻
  • 北京最新消息今天新闻seo问答
  • 怎么做外链到其他网站旺道智能seo系统
  • 做企业销售分析的网站企业网站建站
  • 重启 iis 中的网站北京seo排名服务
  • 中小企业品牌网站建设没干过网络推广能干吗
  • seo服务器seo关键词排名优化怎样收费
  • 网站编程语言有哪些河南百度推广电话
  • html 动漫网站电商网站网址