CODE 6: Word Ladder II

本文探讨了一种算法,用于在给定的词汇集合中找到从一个单词到另一个单词的最短转换路径。算法允许每次只改变一个字母,并确保每一步都存在于词汇集中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

public ArrayList<ArrayList<String>> findLadders(String start, String end,
			HashSet<String> dict) {
		// Start typing your Java solution below
		// DO NOT write main() function
		if (start.equals(end) || dict.size() == 2) {
			ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
			return results;
		}
		
		dict.add(start);
		dict.add(end);

		Map<String, DepthWordsPair> map = new HashMap<String, DepthWordsPair>();
		map.put(start, new DepthWordsPair(1));

		Queue<String> q = new LinkedList<String>();
		int depth = 1;
		int len = start.length(); // word length
		int min_depth = dict.size() + 2; // 2 for start and end words
		q.offer(start);

		while (!q.isEmpty() && min_depth > depth) {
			String cur = q.poll();

			depth = map.get(cur).getDepth();
			for (int i = 0; i < len; ++i) // check word(s)
			{
				StringBuilder sb = new StringBuilder(cur);
				for (char a = 'a'; a <= 'z'; ++a) {
					if (a == cur.charAt(i))
						continue;
					sb.setCharAt(i, a);
					String t = sb.toString();

					if (t.equals(end)) {
						min_depth = depth + 1;
					}

					if (dict.contains(t)) {
						DepthWordsPair dwp = map.get(t);
						if (null == dwp) {
							DepthWordsPair dw = new DepthWordsPair(depth + 1);
							dw.getWords().add(cur);
							map.put(t, dw);
							q.offer(t);
						} else if (dwp.getDepth() == depth + 1) {
							dwp.getWords().add(cur);
						}
					}
				}
			}
		} // end while

		ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
		if (min_depth == dict.size() + 2) { // only for start and end words
			return result;
		}
		Stack<String> stack = new Stack<String>();
		Stack<String> sequence = new Stack<String>();
		stack.push(end);
		while (!stack.isEmpty()) {
			String top = stack.pop();
			sequence.push(top);
			List<String> sons = map.get(top).getWords();
			for (int i = 0; i < sons.size(); ++i) {
				stack.push(sons.get(i));
			}
			if (sons.size() == 0) {
				int index = result.size();
				result.add(new ArrayList<String>());
				for (int i = sequence.size() - 1; i >= 0; --i) {
					result.get(index).add(sequence.get(i)); // save result
				}
				top = sequence.pop();
				while (!sequence.empty()) {
					String father = sequence.peek();
					List<String> brothers = map.get(father).getWords();
					if (!top.equals(brothers.get(0))) {
						break;
					}
					sequence.pop();
					top = father;
				}
			}
		}
		return result;

	}

	class DepthWordsPair {
		int depth;
		List<String> words;

		public DepthWordsPair(int depth) {
			this.depth = depth;
			words = new ArrayList<String>();
		}

		public int getDepth() {
			return depth;
		}

		public void setDepth(int depth) {
			this.depth = depth;
		}

		public List<String> getWords() {
			return words;
		}

		public void setWords(List<String> words) {
			this.words = words;
		}
	}


FUNCTION_BLOCK FB_CleaningVerification VAR_INPUT StartVerification: BOOL; // 启动验证 Conductivity: REAL; // 电导率(μS/cm) TOC_Value: REAL; // TOC值(ppb) BioSample_OK: BOOL; // 微生物采样结果(TRUE=合格) END_VAR VAR_OUTPUT VerificationPassed: BOOL; // 验证通过标志 FailCode: WORD; // 失败代码(0x0001=电导率, 0x0002=TOC, 0x0004=微生物) ValidationReport: STRING[200]; // 验证报告 END_VAR VAR CondLimit: REAL := 1.3; // 电导率阈值 TOCLimit: REAL := 500.0; // TOC阈值 Timer: TON := (PT := T#5M); // 验证超时5分钟 END_VAR // 启动验证流程 IF StartVerification THEN Timer(IN := TRUE); // 并行检测三项指标 VerificationPassed := TRUE; FailCode := 0; // 电导率验证 IF Conductivity > CondLimit THEN VerificationPassed := FALSE; FailCode := FailCode OR 16#0001; END_IF; // TOC验证 IF TOC_Value > TOCLimit THEN VerificationPassed := FALSE; FailCode := FailCode OR 16#0002; END_IF; // 微生物验证(需外部采样器反馈) IF NOT BioSample_OK THEN VerificationPassed := FALSE; FailCode := FailCode OR 16#0004; END_IF; // 生成报告 ValidationReport := CONCAT( "验证结果:", BOOL_TO_STRING(VerificationPassed), " | 电导率:", REAL_TO_STRING(Conductivity,1), "/", REAL_TO_STRING(CondLimit,1), " | TOC:", REAL_TO_STRING(TOC_Value,0), "ppb/", REAL_TO_STRING(TOCLimit,0), " | 微生物:", BOOL_TO_STRING(BioSample_OK) ); // 超时处理 IF Timer.Q THEN VerificationPassed := FALSE; FailCode := FailCode OR 16#0008; // 超时错误码 END_IF; ELSE VerificationPassed := FALSE; FailCode := 0; END_IF; END_FUNCTION_BLOCK 根据上述内容改为梯形图
04-01
一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值