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

代码随想录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){
        //不能放在遍历里面,否则刚进来第一组没办法变成true
        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 || 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.kler.cn/a/320238.html

相关文章:

  • 【微软:多模态基础模型】(5)多模态大模型:通过LLM训练
  • Nuxt3 动态路由URL不更改的前提下参数更新,NuxtLink不刷新不跳转,生命周期无响应解决方案
  • vue 常用特性 ( 计算属性 | 侦听器 | 过滤器 )
  • 机器翻译-基础与模型
  • 4-7-1.C# 数据容器 - LinkedList(LinkedList 的定义、LinkedList 结点的遍历、LinkedList 的常用方法)
  • 【经验分享】2024年11月下半年软件设计师考试选择题估分(持续更新~~)
  • Spring Boot 点餐系统:餐饮界的技术革新
  • Packet Tracer - IPv4 ACL 的实施挑战(完美解析)
  • 【C++笔试强训】如何成为算法糕手Day3
  • Linux标准I/O
  • (11)(2.1.2) DShot ESCs(四)
  • 学校快递站点管理|基于springboot学校快递站点管理设计与实现(源码+数据库+文档)
  • 屠龙之人终成恶龙
  • 页面禁用鼠标右键属于反爬虫措施吗 ?
  • 《飞机大战游戏》实训项目(Java GUI实现)(设计模式)(简易)
  • L2 Loss介绍及梯度计算说明
  • 2、.Net 前端框架:Blazor - .Net宣传系列文章
  • OJ在线评测系统 后端开发数据库初始化工作 开发库表 建立数据库索引 Mybatis映射初始化接口开发
  • 自动驾驶系列—盲点检测(BSD)功能:智能驾驶安全的关键保障
  • 14年数据结构
  • oracle direct path read处理过程
  • 接口调用工具-HttpClient,HttpUtil,RestTemplate
  • Spring Security - 用户授权
  • 1数据结构与算法-前言
  • OpenCV图像文件读写(3)统计多页图像文件中的页面数量函数imcount()的使用
  • 机器学习中的元强化学习