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});
				}

			}
		}
		
	}
相关推荐
No0d1es6 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
大阳1238 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
weixin_3077791310 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
学行库小秘10 小时前
ANN神经网络回归预测模型
人工智能·python·深度学习·神经网络·算法·机器学习·回归
没落之殇10 小时前
基于C语言实现的HRV分析方法 —— 与Kubios和MATLAB对比
算法
秋难降10 小时前
线段树的深度解析(最长递增子序列类解题步骤)
数据结构·python·算法
楚韵天工11 小时前
基于GIS的无人机模拟飞行控制系统设计与实现
深度学习·算法·深度优先·无人机·广度优先·迭代加深·图搜索算法
你也向往长安城吗12 小时前
推荐一个三维导航库:three-pathfinding-3d
javascript·算法
百度智能云12 小时前
VectorDB+FastGPT一站式构建:智能知识库与企业级对话系统实战
算法
AI小白的Python之路13 小时前
数据结构与算法-排序
数据结构·算法·排序算法