DAY52-图论BFS

kama101.孤岛的总面积

java 复制代码
	/**
	 * 在外循环遇到没有标记过的岛屿+1
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt();
		int m=scan.nextInt();
		
		int[][] isoland = new int[n][m];
		boolean[][] path = new boolean[n][m];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				isoland[i][j]=scan.nextInt();
			}
		}
		
		//初始化_把连着四周的陆地变为海洋
		for(int i=0;i<m;i++) {
			if(isoland[0][i]==1&&path[0][i]==false) {
				bfs0(isoland,path,0,i,n,m);
			}
			if(isoland[n-1][i]==1&&path[n-1][i]==false) {
				bfs0(isoland,path,n-1,i,n,m);
			}
		}
		for(int i=1;i<n-1;i++) {
			if(isoland[i][0]==1&&path[i][0]==false) {
				bfs0(isoland,path,i,0,n,m);
			}
			if(isoland[i][m-1]==1&&path[i][m-1]==false) {
				bfs0(isoland,path,i,m-1,n,m);
			}
		}
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				System.out.print(isoland[i][j]);
			}
			System.out.println();
		}
		
		//DFS搜索
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(isoland[i][j]==1&&path[i][j]==false) {
					bfs(isoland,path,i,j,n,m);
				}
			}
		}
		
		System.out.println(sum);
		scan.close();
	}
	
	public static int sum=0;
	public static int[][] step = {{1,0},{0,1},{-1,0},{0,-1}};
	
	/**
	 * 初始化为0
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs0(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		isoland[x][y]=0;
		
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]==1&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					isoland[tempx][tempy]=0;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}
	
	/**
	 * 从哪个点开始广度搜索
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		sum++;
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]==1&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					sum++;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}
	

kama102.沉没孤岛

java 复制代码
	/**
	 * 在外循环遇到没有标记过的岛屿+1
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt();
		int m=scan.nextInt();
		
		int[][] isoland = new int[n][m];
		boolean[][] path = new boolean[n][m];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				isoland[i][j]=scan.nextInt();
			}
		}
		
		//初始化_把连着四周的陆地变为海洋
		for(int i=0;i<m;i++) {
			if(isoland[0][i]==1&&path[0][i]==false) {
				bfs(isoland,path,0,i,n,m);
			}
			if(isoland[n-1][i]==1&&path[n-1][i]==false) {
				bfs(isoland,path,n-1,i,n,m);
			}
		}
		for(int i=1;i<n-1;i++) {
			if(isoland[i][0]==1&&path[i][0]==false) {
				bfs(isoland,path,i,0,n,m);
			}
			if(isoland[i][m-1]==1&&path[i][m-1]==false) {
				bfs(isoland,path,i,m-1,n,m);
			}
		}
		

		
		//DFS搜索
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(isoland[i][j]==1&&path[i][j]==false) {
					bfs0(isoland,path,i,j,n,m);
				}
			}
		}
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				System.out.print(isoland[i][j]);
				if(j!=m-1) {
					System.out.print(" ");
				}
			}
			if(i!=n-1) {
				System.out.println();			
			}
		}

		scan.close();
	}
	
	public static int[][] step = {{1,0},{0,1},{-1,0},{0,-1}};
	
	/**
	 * 初始化为0
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs0(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		isoland[x][y]=0;
		
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]==1&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					isoland[tempx][tempy]=0;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}
	
	/**
	 * 从哪个点开始广度搜索
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]==1&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}

kama103.水流问题

java 复制代码
	/**
	 *从四周遍历获取两边都可以到达的地方
	 * @param args
	 */
	public static void main(String[] args) {
		
		//获取岛屿数组
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt();
		int m=scan.nextInt();
		
		int[][] isoland = new int[n][m];
		boolean[][] pathL = new boolean[n][m];
		boolean[][] pathR = new boolean[n][m];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				isoland[i][j]=scan.nextInt();
			}
		}
		
		//遍历上边界和下边界
		for(int i=0;i<m;i++) {
			if(pathL[0][i]==false) {
				bfs(isoland,pathL,0,i,n,m);
			}
			if(pathR[n-1][i]==false) {
				bfs(isoland,pathR,n-1,i,n,m);
			}
		}
		//遍历左边界和右边界
		for(int i=0;i<n;i++) {
			if(pathL[i][0]==false) {
				bfs(isoland,pathL,i,0,n,m);
			}
			if(pathR[i][m-1]==false) {
				bfs(isoland,pathR,i,m-1,n,m);
			}
		}
		
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(pathL[i][j]==true&&pathR[i][j]==true) {
					System.out.println(i+" "+j);
				}
			}
		}

		scan.close();
	}
	
	public static int[][] step = {{1,0},{0,1},{-1,0},{0,-1}};
	
	
	/**
	 * 从哪个点开始广度搜索
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]>=isoland[temp[0]][temp[1]]&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}

kama104.建造最大岛屿

java 复制代码
	/**
	 * 遍历每个岛并编号、再遍历所有的0将0周围的所有值加起来
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt();
		int m=scan.nextInt();
		
		int[][] isoland = new int[n][m];
		boolean[][] path = new boolean[n][m];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				isoland[i][j]=scan.nextInt();
			}
		}


		//BFS遍历
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(isoland[i][j]==1&&path[i][j]==false) {
					bfs0(isoland,path,i,j,n,m);
					hash.put(count, area);
					count++;
					area=0;
				}
			}
		}
		
		
		//会出现重复计算一个岛屿的问题
		int max=0;
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				
				//如果该位置是海洋
				if(isoland[i][j]==0) {
					int sub=0;
					set.clear();
					for(int k=0;k<4;k++) {
						int tempx=i+step[k][0];
						int tempy=j+step[k][1];
						
						//越界
						if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
						if(isoland[tempx][tempy]==0) continue;
						if(set.contains(isoland[tempx][tempy]))continue;
						sub=sub+hash.get(isoland[tempx][tempy]);
						set.add(isoland[tempx][tempy]);
					}
					sub++;
					if(sub>max)max=sub;
				}
			}
		}
		
		scan.close();
		//说明没有海洋部分,返回整个陆地部分
		if(max==0) {
			System.out.println(hash.get(2));
			return ;
		}
		System.out.println(max);
	}
	
	public static int[][] step = {{1,0},{0,1},{-1,0},{0,-1}};
	
	//编号与map
	public static Map<Integer,Integer> hash = new HashMap<>();
	public static Set<Integer> set = new HashSet<>();
	public static int count=2;
	public static int area=0;
	/**
	 * 初始化为0
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs0(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		isoland[x][y]=count;
		area++;
		
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]==1&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					isoland[tempx][tempy]=count;
					area++;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}
	
	/**
	 * 从哪个点开始广度搜索
	 * @param isoland
	 * @param path
	 * @param x
	 * @param y
	 */
	public static void bfs(int[][] isoland,boolean[][] path,int x,int y,int n,int m) {
		LinkedList<int[]> queue = new LinkedList<>();
		queue.add(new int[]{x,y});
		path[x][y]=true;
		while(!queue.isEmpty()) {
			int[] temp = queue.remove();
			
			for(int i=0;i<4;i++) {
				int tempx=temp[0]+step[i][0];
				int tempy=temp[1]+step[i][1];
				
				//越界
				if(tempx<0||tempx>n-1||tempy<0||tempy>m-1) continue;
				if(isoland[tempx][tempy]==1&&path[tempx][tempy]==false) {
					path[tempx][tempy]=true;
					queue.add(new int[] {tempx,tempy});
				}

			}
		}
		
	}
相关推荐
WWZZ20252 分钟前
快速上手大模型:机器学习3(多元线性回归及梯度、向量化、正规方程)
人工智能·算法·机器学习·机器人·slam·具身感知
东方佑1 小时前
从字符串中提取重复子串的Python算法解析
windows·python·算法
西阳未落1 小时前
LeetCode——二分(进阶)
算法·leetcode·职场和发展
通信小呆呆2 小时前
以矩阵视角统一理解:外积、Kronecker 积与 Khatri–Rao 积(含MATLAB可视化)
线性代数·算法·matlab·矩阵·信号处理
CoderCodingNo3 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
一个不知名程序员www3 小时前
算法学习入门---双指针(C++)
c++·算法
Shilong Wang3 小时前
MLE, MAP, Full Bayes
人工智能·算法·机器学习
Theodore_10223 小时前
机器学习(6)特征工程与多项式回归
深度学习·算法·机器学习·数据分析·多项式回归
知花实央l3 小时前
【算法与数据结构】拓扑排序实战(栈+邻接表+环判断,附可运行代码)
数据结构·算法
lingling0094 小时前
机械臂动作捕捉系统选型指南:从需求到方案,NOKOV 度量光学动捕成优选
人工智能·算法