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

			}
		}
		
	}
相关推荐
KingRumn4 小时前
Linux信号之标准信号与实时信号
linux·算法
源代码•宸8 小时前
Leetcode—620. 有趣的电影&&Q3. 有趣的电影【简单】
数据库·后端·mysql·算法·leetcode·职场和发展
2301_800256118 小时前
地理空间数据库中的CPU 和 I/O 开销
数据库·算法·oracle
一个不知名程序员www9 小时前
算法学习入门---结构体和类(C++)
c++·算法
XFF不秃头11 小时前
力扣刷题笔记-旋转图像
c++·笔记·算法·leetcode
王老师青少年编程11 小时前
csp信奥赛C++标准模板库STL案例应用3
c++·算法·stl·csp·信奥赛·lower_bound·标准模版库
有为少年12 小时前
Welford 算法 | 优雅地计算海量数据的均值与方差
人工智能·深度学习·神经网络·学习·算法·机器学习·均值算法
Ven%12 小时前
从单轮问答到连贯对话:RAG多轮对话技术详解
人工智能·python·深度学习·神经网络·算法
山楂树の12 小时前
爬楼梯(动态规划)
算法·动态规划