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

			}
		}
		
	}
相关推荐
人才程序员3 分钟前
CSP-J 算法基础 广度优先搜索BFS
数据结构·c++·算法·深度优先·宽度优先·比赛·noi
GZK.6 分钟前
【Leetcode】70. 爬楼梯
算法·leetcode·动态规划
redcocal2 小时前
地平线内推码 kbrfck
c++·嵌入式硬件·mcu·算法·fpga开发·求职招聘
西农小陈3 小时前
python-字符排列问题
数据结构·python·算法
TechQuester3 小时前
OpenAI 刚刚推出 o1 大模型!!突破LLM极限
人工智能·python·gpt·算法·chatgpt
西农小陈3 小时前
python-简单的数据结构
数据结构·python·算法
_Chocolate3 小时前
十大排序(一):冒泡排序
c语言·数据结构·算法
running thunderbolt4 小时前
C++:类和对象全解
c语言·开发语言·c++·算法
小陈的进阶之路5 小时前
c++刷题
开发语言·c++·算法
model20055 小时前
sahi目标检测java实现
java·算法·目标检测