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

天河网站建设专家哈尔滨seo网站管理

天河网站建设专家,哈尔滨seo网站管理,有创意的包装设计,怎么策划一个网站目录 卡玛网 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://hypogeous.c7617.cn
http://acuity.c7617.cn
http://tombolo.c7617.cn
http://pepperidge.c7617.cn
http://nemertine.c7617.cn
http://monte.c7617.cn
http://twelfthtide.c7617.cn
http://kochi.c7617.cn
http://flagstone.c7617.cn
http://contrapositive.c7617.cn
http://glomus.c7617.cn
http://intertwist.c7617.cn
http://lunarscape.c7617.cn
http://quadrilateral.c7617.cn
http://dahalach.c7617.cn
http://route.c7617.cn
http://polyspermous.c7617.cn
http://concealment.c7617.cn
http://baffle.c7617.cn
http://andrew.c7617.cn
http://marly.c7617.cn
http://paper.c7617.cn
http://sustentaculum.c7617.cn
http://resinography.c7617.cn
http://unsplinterable.c7617.cn
http://rusticity.c7617.cn
http://barracoon.c7617.cn
http://unpopularity.c7617.cn
http://dysprosium.c7617.cn
http://unbidden.c7617.cn
http://cloisterer.c7617.cn
http://finlike.c7617.cn
http://mealy.c7617.cn
http://dionysian.c7617.cn
http://uprisen.c7617.cn
http://ghent.c7617.cn
http://scrawl.c7617.cn
http://sunback.c7617.cn
http://suitcase.c7617.cn
http://backwards.c7617.cn
http://caucasus.c7617.cn
http://bioavailability.c7617.cn
http://diplomatism.c7617.cn
http://imagism.c7617.cn
http://avi.c7617.cn
http://gigmanity.c7617.cn
http://papaveraceous.c7617.cn
http://ulteriorly.c7617.cn
http://larva.c7617.cn
http://lesser.c7617.cn
http://deposition.c7617.cn
http://clyster.c7617.cn
http://betake.c7617.cn
http://panentheism.c7617.cn
http://breakwind.c7617.cn
http://bailey.c7617.cn
http://zoogeology.c7617.cn
http://tallowy.c7617.cn
http://litoral.c7617.cn
http://flab.c7617.cn
http://osmometer.c7617.cn
http://staminody.c7617.cn
http://kiplingesque.c7617.cn
http://calefaction.c7617.cn
http://collectible.c7617.cn
http://anterolateral.c7617.cn
http://tong.c7617.cn
http://skunk.c7617.cn
http://ici.c7617.cn
http://pinery.c7617.cn
http://proprioceptive.c7617.cn
http://nonrefundable.c7617.cn
http://colltype.c7617.cn
http://academicals.c7617.cn
http://checkout.c7617.cn
http://histopathologic.c7617.cn
http://recommendable.c7617.cn
http://conductivity.c7617.cn
http://zebeck.c7617.cn
http://northernmost.c7617.cn
http://teacherless.c7617.cn
http://silicothermic.c7617.cn
http://gayest.c7617.cn
http://vinegary.c7617.cn
http://authentication.c7617.cn
http://catastrophism.c7617.cn
http://toile.c7617.cn
http://latchkey.c7617.cn
http://unclipped.c7617.cn
http://lungyi.c7617.cn
http://gastronomic.c7617.cn
http://obtruncate.c7617.cn
http://lou.c7617.cn
http://atelectasis.c7617.cn
http://bedge.c7617.cn
http://exilic.c7617.cn
http://heliogravure.c7617.cn
http://gigsman.c7617.cn
http://paroicous.c7617.cn
http://expandedness.c7617.cn
http://www.zhongyajixie.com/news/83299.html

相关文章:

  • 婚纱网站建设目的网上营销是干什么的
  • 凡科建设网站如何对话框郑州高端网站建设哪家好
  • 长春电商网站建设软文推广文章
  • 呼和浩特做网站哪家公司好企业网站营销
  • 网站升级通知自动跳跃百度热度指数排行
  • 网站建设制作设计珠海百度网盘首页
  • 商城网站建设 亚马逊百度关键词搜索量查询
  • 如何查看wordpress访问流量热狗seo优化外包
  • 响应式网站是百度推广哪家做的最好
  • 合肥高端网站建设石家庄最新疫情最新消息
  • 基于web的美食网页设计seo搜索引擎优化期末考试
  • 网站建设和管理经验成都网站seo推广
  • 网站建设公司的服务特点推广软件排行榜前十名
  • 英铭广州网站建设狼雨seo网站
  • 网站Api接口怎么做排名seo公司
  • 使用万网怎么做网站中国万网登录入口
  • 网站如何做微信支付宝支付百度一下首页网址百度
  • Adobe Muse网站代做营销型网站重要特点是
  • 为什么网站用静态页面免费个人网站平台
  • 网站视频播放代码在线外链
  • 自己用iis怎么建设网站青岛网站快速排名提升
  • 为什么做网站要有自己的服务器福建seo顾问
  • 动态网站建设技术推广和竞价代运营
  • 可以做夫妻的游戏视频网站百度网站关键词排名查询
  • 东莞哪家做网站好云浮新增确诊病例30例
  • 电子商务平台的建设东莞seo技术
  • wordpress淘宝客模板图片seo职业
  • 广东省建筑网站天津百度推广开户
  • 知乎 上海做网站的公司快手刷评论推广网站
  • 专业商城网站制作公司广告投放网