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

			}
		}
		
	}
相关推荐
我爱C编程38 分钟前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7891 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
chao_7891 小时前
Selenium 自动化实战技巧【selenium】
自动化测试·selenium·算法·自动化
YuTaoShao1 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
怀旧,1 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
泛舟起晶浪1 小时前
相对成功与相对失败--dp
算法·动态规划·图论
地平线开发者2 小时前
地平线走进武汉理工,共建智能驾驶繁荣生态
算法·自动驾驶
IRevers2 小时前
【自动驾驶】经典LSS算法解析——深度估计
人工智能·python·深度学习·算法·机器学习·自动驾驶
前端拿破轮2 小时前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试
凤年徐2 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题