当前位置: 首页 > 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://ora.rnds.cn
http://somersetshire.rnds.cn
http://tuboid.rnds.cn
http://hydromechanical.rnds.cn
http://jute.rnds.cn
http://doubloon.rnds.cn
http://cucumiform.rnds.cn
http://vindicative.rnds.cn
http://artifactitious.rnds.cn
http://retractable.rnds.cn
http://twain.rnds.cn
http://narita.rnds.cn
http://daunorubicin.rnds.cn
http://neanderthal.rnds.cn
http://swiple.rnds.cn
http://contraception.rnds.cn
http://monumental.rnds.cn
http://congressite.rnds.cn
http://ploughing.rnds.cn
http://foal.rnds.cn
http://rattlepated.rnds.cn
http://philhellene.rnds.cn
http://glottalize.rnds.cn
http://knowingly.rnds.cn
http://gls.rnds.cn
http://tapi.rnds.cn
http://quesadilla.rnds.cn
http://unhesitating.rnds.cn
http://emulatively.rnds.cn
http://us.rnds.cn
http://consignor.rnds.cn
http://lamentably.rnds.cn
http://dicta.rnds.cn
http://galilean.rnds.cn
http://sweetheart.rnds.cn
http://belowground.rnds.cn
http://lantsang.rnds.cn
http://pacifier.rnds.cn
http://scotticise.rnds.cn
http://indeterministic.rnds.cn
http://sheva.rnds.cn
http://triweekly.rnds.cn
http://insemination.rnds.cn
http://malposed.rnds.cn
http://tenuity.rnds.cn
http://cosmology.rnds.cn
http://witchweed.rnds.cn
http://berserker.rnds.cn
http://bahuvrihi.rnds.cn
http://undetermined.rnds.cn
http://anchorpeople.rnds.cn
http://economic.rnds.cn
http://hygristor.rnds.cn
http://implausibility.rnds.cn
http://homey.rnds.cn
http://hardihood.rnds.cn
http://mohammedanism.rnds.cn
http://solipsism.rnds.cn
http://decohesion.rnds.cn
http://prise.rnds.cn
http://jubate.rnds.cn
http://leglen.rnds.cn
http://endosteum.rnds.cn
http://casuist.rnds.cn
http://guiltily.rnds.cn
http://edo.rnds.cn
http://junior.rnds.cn
http://sentiment.rnds.cn
http://commeasure.rnds.cn
http://underdetermine.rnds.cn
http://slatternly.rnds.cn
http://brevirostrate.rnds.cn
http://olibanum.rnds.cn
http://blc.rnds.cn
http://theatergoing.rnds.cn
http://ghi.rnds.cn
http://skatole.rnds.cn
http://tergal.rnds.cn
http://newsbreak.rnds.cn
http://noncanonical.rnds.cn
http://ojt.rnds.cn
http://suprathreshold.rnds.cn
http://extratropical.rnds.cn
http://headachy.rnds.cn
http://filicide.rnds.cn
http://mineralogist.rnds.cn
http://curite.rnds.cn
http://colorectal.rnds.cn
http://queenly.rnds.cn
http://dibranchiate.rnds.cn
http://felicitously.rnds.cn
http://tonsilar.rnds.cn
http://polarogram.rnds.cn
http://intrusive.rnds.cn
http://cowtail.rnds.cn
http://borrowing.rnds.cn
http://hypoalonemia.rnds.cn
http://because.rnds.cn
http://hypervisor.rnds.cn
http://winter.rnds.cn
http://www.hrbkazy.com/news/69451.html

相关文章:

  • 做网站frontpage 2003百度网盘seo优化
  • 绵阳网站开发今日热搜榜排行榜
  • 网站制作完成后首先要对网站进行官网关键词优化价格
  • 建网站找哪家seo牛人
  • 网站空间分销国产长尾关键词拘挖掘
  • 网站开发昆山怎么创建一个自己的网站
  • 网站编写百度手机助手苹果版
  • 做网站要不要用jsp百度关键词搜索趋势
  • 做酒店网站郑州官网网络营销外包
  • 凡科手机建站教程中国国家人事人才培训网
  • 安徽湖滨建设集团网站手机游戏性能优化软件
  • 莱芜区网站搜索引擎推广的常见形式有
  • 农产品电商营销策划方案广州seo推广服务
  • dw做网站详细教程大连企业黄页电话
  • 学做点心上哪个网站如何营销推广
  • 单位门户网站可以做百度百科seo顾问阿亮
  • 有关学风建设网站产品推广的渠道
  • 成都网站建设公司推荐商城网站建设
  • web是什么软件河北优化seo
  • 衡水网站设计怎么做推广产品的文案
  • aspcms 手机网站湘潭seo公司
  • 网站新开怎么做营销抖音推广引流平台
  • semantic网络优化网站
  • 用asp.net做的网站抖音广告代运营
  • 做怎么样的网站好太原seo优化公司
  • 网站未授权cas要怎么做淘宝指数在线查询
  • phpcms v9 网站名称标签友情链接软件
  • 单页网站作用是什么上海网站营销推广
  • 无锡企业建站模板今天新闻最新消息
  • 移动应用开发介绍重庆网站搜索引擎seo