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

代码随想录Day53|102.沉没孤岛 、103.水流问题 、104.建造最大岛屿

102.沉没孤岛  

import java.util.*;class Main{public static 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[][] grid = new int[n][m];for(int i = 0; i <n; i++){for(int j = 0; j<m; j++){grid[i][j] = sc.nextInt();}}boolean[][] visited = new boolean[n][m];for(int i = 0; i < n; i++){if(grid[i][0] == 1 && visited[i][0] == false){dfs(grid,visited,i,0);}if(grid[i][m-1] == 1 && visited[i][m-1] == false){dfs(grid,visited,i,m-1);}}//遍历上下边界for(int j = 0; j <= m-1; j++){if(grid[0][j] == 1 && visited[0][j] == false){dfs(grid,visited,0,j);}if(grid[n-1][j] == 1 && visited[n-1][j] == false){dfs(grid,visited,n-1,j);}}for(int i = 0; i < n; i++){for(int j = 0; j< m; j++){if(grid[i][j] == 1) grid[i][j] = 0;if(grid[i][j] == 2) grid[i][j] = 1;}}for(int i = 0; i < n; i++ ){for(int j = 0; j< m; j++){System.out.print(grid[i][j]+" ");}System.out.println();}}public static void dfs(int[][] grid, boolean[][] visited, int x,int y){if(visited[x][y] == true || grid[x][y] == 0){return;}grid[x][y] = 2;visited[x][y] = true;for(int i = 0; i<4; i++){int nextX = x + dir[i][0];int nextY = y + dir[i][1];if(nextY < 0 || nextY >= grid[0].length || nextX < 0 || nextX >= grid.length){continue;}dfs(grid,visited,nextX,nextY);}}
}

103.水流问题 

import java.util.*;class Main{public static int[][] dir = {{1,0},{0,1},{-1,0},{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();}}//初始化边界boolean[][] firstBound = new boolean[n][m],secondBound = new boolean[n][m];//遍历左边界和有边界for(int i = 0; i < n; i++){dfs(grid,i,0,firstBound);dfs(grid,i,m-1,secondBound);}//遍历上边界和下边界for(int i = 0; i <m; i++){dfs(grid,0,i,firstBound);dfs(grid,n-1,i,secondBound);}for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(firstBound[i][j] && secondBound[i][j]){System.out.println(i+" " + j);}}}}public static void dfs(int[][] grid,int x,int y,boolean[][] visited){//不能放在遍历里面,否则刚进来第一组没办法变成truevisited[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 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length || grid[nextX][nextY] < grid[x][y] ||visited[nextX][nextY]){continue;}dfs(grid,nextX,nextY,visited);}}
}

104.建造最大岛屿

import java.util.*;class Main{public static int[][] dirs = {{1,0},{0,1},{-1,0},{0,-1}};static int count = 0;static int tag = 2;public static void main (String[] args) {/* code */Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] grid = new int[n][m];boolean[][] visited = new boolean[n][m];for(int i = 0; i< n; i++){for(int j = 0; j< m; j++){grid[i][j] = sc.nextInt();}}boolean totalIsland = true;//遍历记录面积Map<Integer,Integer> countArea = new HashMap<>();for(int i = 0; i<n; i++){for(int j = 0; j< m; j++){if(grid[i][j] == 0) totalIsland = false;if( !visited[i][j] && grid[i][j] != 0){count = 0;dfs(grid,visited,i,j);countArea.put(tag++,count);}}}/*/打印数组for(int i = 0; i< n; i++){for(int j = 0; j< m; j++){System.out.print(grid[i][j] + " ");}System.out.println();}//*/int res = 1;HashSet<Integer> island = new HashSet<>();int flag = 0;//再次遍历添加小岛for(int i = 0; i<n; i++){for(int j = 0; j< m; j++){if(grid[i][j] == 0){int area = 1;island.clear();for(int dir[]:dirs){int nextX = i + dir[0];int nextY = j + dir[1];if(nextX < 0 || nextX >= grid.length || nextY < 0|| nextY >= grid[0].length) continue;flag =grid[nextX][nextY];if(!island.contains(flag) && countArea.containsKey(flag)){area+=countArea.get(flag);island.add(flag);}}res = Math.max(area,res);}}}if(totalIsland == false){System.out.println(res);}else{System.out.println(m*n);}}public static void dfs(int[][] grid, boolean[][] visited, int x, int y){visited[x][y] = true;grid[x][y] = tag;count++;for(int[] dir: dirs){int nextX = x + dir[0];int nextY = y + dir[1];if(nextX < 0 || nextX >= grid.length || nextY < 0|| nextY >= grid[0].length || visited[nextX][nextY] || grid[nextX][nextY] == 0){continue;}dfs(grid, visited, nextX, nextY);}}
}

http://www.lryc.cn/news/446654.html

相关文章:

  • 19c-pfile
  • 智能软件开启精准品牌控价
  • OpenCV特征检测(8)检测图像中圆形的函数HoughCircles()的使用
  • spark 大表与大表join时的Shuffle机制和过程
  • 大厂面试真题:简单说下Redis的bigkey
  • 18 vue3之自动引入ref插件深入使用v-model
  • 【Spring】lombok、dbUtil插件应用
  • 【学习笔记】WSL
  • python assert 断言用法
  • MySQL事务、索引、数据恢复和备份
  • 什么是chatgpt?国内有哪些类gpt模型?
  • ISP基本框架及算法介绍 ISP(Image Signal Processor)
  • Stable Diffusion 的 ControlNet 主要用途
  • 矩阵分析 学习笔记4 内积与Gram矩阵
  • iOS 消息机制详解
  • 深入理解Spring Data JPA与接口编程
  • Wireshark学习使用记录
  • OpenCV特征检测(9)检测图像中直线的函数HoughLines()的使用
  • 力扣 中等 445.两数相加 II
  • 华为云徐峰:AI赋能应用现代化,加速软件生产力跃升
  • C发送邮件技巧:如何批量发送个性化邮件?
  • 基于python+spark的外卖餐饮数据分析系统设计与实现(含论文)-Spark毕业设计选题推荐
  • 权限维持——Linux
  • 申请SSL证书闭坑方法
  • linux 下域名解析错误
  • 基于单片机的角度、水位、温度、辅助热源、电机仿真
  • 泛函分析精解【1】
  • 大数据毕业设计选题推荐-租房数据分析系统-Hive-Hadoop-Spark
  • 有关shell指令练习2
  • Exception与Error:Java中的异常处理