DAY53-图论BFS

kama110.字符串接龙

java 复制代码
	/**
	 * 遍历每个字母和每个位置替换
	 * @param args
	 */
	public static void main(String[] args) {
		//读取
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		scan.nextLine();
		String startStr=scan.next();
		String endStr=scan.next();
		scan.nextLine();
		
		//set集合来存储字符串
		Set<String> set = new HashSet<>();
		for(int i=0;i<n;i++) {
			String s = scan.nextLine();
			set.add(s);
		}

		int res = countPath(set,startStr,endStr);
		System.out.println(res);
		scan.close();
	}
	
	public static int countPath(Set<String> set,String startStr,String endStr) {
		//path集合来标记走过的路径,BFS是向外扩散,无向图需要记录不走回头路
		Map<String,Integer> map = new HashMap<>();
		map.put(startStr, 1);
		
		//创造队列
		Queue<String> queue = new LinkedList<>();
		queue.offer(startStr);
		int path=0;
		
		while(!queue.isEmpty()) {
			String str = queue.poll();
			path = map.get(str);
			
			for(int i=0;i<str.length();i++) {
				char[] str1 = str.toCharArray();
				//遍历26个字母求得新字符串
				for(char j='a';j<='z';j++) {
					str1[i]=j;
					String newStr = new String(str1);
					
					//当走到end字段则返回
					if(newStr.equals(endStr)) return path+1;
					
					//将路径加入map中
					if(set.contains(newStr)&&!map.containsKey(newStr)) {
						map.put(newStr,path+1);
						queue.offer(newStr);
					}
				}
			}
		}
		
		return 0;
	}

kama105.有向图的完全可达性

java 复制代码
	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+1][n+1];
		
		for(int i=0;i<m;i++) {
			int x = scan.nextInt();
			int y = scan.nextInt();
			isoland[x][y]=1;
		}
		
		Queue<Integer> queue = new LinkedList<>();
		queue.add(1);
		
		Set<Integer> set = new HashSet<>();
		set.add(1);
		
		while(!queue.isEmpty()) {
			int now = queue.remove();
			
			for(int i=1;i<=n;i++) {
				if(isoland[now][i]==1&&!set.contains(i)) {
					set.add(i);
					queue.add(i);
				}
			}
		}
		
		if(set.size()==n) {
			System.out.println(1);
		}else {
			System.out.println(-1);
		}
		
		scan.close();
	}

kama106.岛屿的周长

java 复制代码
	public static int[][] step = {{1,0},{0,1},{-1,0},{0,-1}};
	
	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];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				isoland[i][j]=scan.nextInt();
			}
		}
		int sum=0;
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				//如果该位置是岛屿,计算边长
				if(isoland[i][j]==1) {
					int count=0;
					//上下左右
					for(int k=0;k<4;k++) {
						int k1=i+step[k][0];
						int k2=j+step[k][1];
						
						if(k1<0||k1>n-1||k2<0||k2>m-1) {
							count++;
						}else {
							if(isoland[k1][k2]==0)count++;
						}
					}
					sum+=count;
				}
			}
		}
		System.out.println(sum);
		scan.close();
	}
相关推荐
大胆飞猪2 小时前
递归、剪枝、回溯算法---全排列、子集问题(力扣.46,78)
算法·leetcode·剪枝
Kisorge4 小时前
【电机控制】基于STM32F103C8T6的二轮平衡车设计——LQR线性二次线控制器(算法篇)
stm32·嵌入式硬件·算法
铭哥的编程日记5 小时前
深入浅出蓝桥杯:算法基础概念与实战应用(二)基础算法(下)
算法·职场和发展·蓝桥杯
Swift社区5 小时前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
cici158745 小时前
基于高光谱成像和偏最小二乘法(PLS)的苹果糖度检测MATLAB实现
算法·matlab·最小二乘法
StarPrayers.7 小时前
自蒸馏学习方法
人工智能·算法·学习方法
大锦终7 小时前
【动规】背包问题
c++·算法·动态规划
智者知已应修善业7 小时前
【c语言蓝桥杯计算卡片题】2023-2-12
c语言·c++·经验分享·笔记·算法·蓝桥杯
hansang_IR8 小时前
【题解】洛谷 P2330 [SCOI2005] 繁忙的都市 [生成树]
c++·算法·最小生成树
多喝开水少熬夜8 小时前
树与图的深度和广度优先遍历-java实现邻接表存储
java·深度优先·宽度优先