SOJ 2325:Word Transformation _floyd

本文介绍了一种利用字典实现单词变换的最短路径算法,通过构造图模型并采用Floyd算法求解从一个单词到另一个单词所需的最少变换步骤。适用于解决如NPR周日广播中常见的单词谜题。

A common word puzzle found in many newspapers and magazines is the word transformation. By taking a starting word and successively altering a single letter to make a new word, one can build a sequence of words which changes the original word to a given end word. For instance, the word ``spice'' can be transformed in four steps to the word ``stock'' according to the following sequence: spice, slice, slick, stick, stock. Each successive word differs from the previous word in only a single character position while the word length remains the same.

Given a dictionary of words from which to make transformations, plus a list of starting and ending words, your team is to write a program to determine the number of steps in the shortest possible transformation.

The input will be a single file in two sections. The first section will be the dictionary of available words with one word per line, terminated by a line containing an asterisk (*) rather than a word. There can be up to 200 words in the dictionary; all words will be in lower case, and no word will be longer than ten characters an no two words are the same. Words can appear in the dictionary in any order.

Following the dictionary are pairs of words, one pair per line, with the words in the pair separated by a single space. These pairs represent the starting and ending words ( possibly the same )in a transformation. The pairs are terminated by the end-of-file. If it's impossible ,output"Impossible". Also words are both in the dictionary .

The output should contain one line per word pair, and must include the starting word, the ending word, and the number of steps in the shortest possible transformation, separated by single spaces.

(For those people who listen to National Public Radio (NPR), the Sunday morning broadcast Weekend Edition, Sunday usually has a word puzzle for the listening audience. Occasionally the puzzle is a transformation. NPR allows the submission of puzzle answers via e-mail, to the address wesun@npr.org. You can use this code to generate solutions to the transformation puzzles and possibly get a chance to be on the air for the live puzzle round.)


Sample Input
dip
lip
mad
map
maple
may
pad
pip
pod
pop
sap
sip
slice
slick
spice
stick
stock
*
spice stock
may pod

Sample Output
spice stock 4
may pod 3


Source:

Southern California Regional of the ACM International Collegiate Programming Contest


//川大个人赛上的一道题,题意是利用一开始给出的字典(“*”前面的所有字符串就是字典里面的),查找给出的两个字符串("*"后面的)第一个要经过多少次变换能够得到第二个字符串。变换过程需满足:1、这两个字符长度相同;2、每次只能变换其中一个字符;3、每一次变换得到的字符串都必须在之前给的字典里面。

//分析:由于要求每次变换过程中要求 中介 字符串都必须在字典里面,换个想法就是说我们将每个字符串看成一个点,那么某个字符串(点)变换(走)的过程中也必须在这些点上面,也就相当于选择一条最短路径了。 那么可以预先将字典里面两两字符串之间需要变换的次数算出来,最后进行最短路。题目后有多次访问,而字符串个数最多才200个,所以选择floyd最佳。 需要注意的是:题目要求每次 只能变换字符串中的一个字符,所有只有两个字符串(长度相同)只有一个字符不同时才建立单位1的路径,否则为0xfffffff。

#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
using namespace std;
#define  maxn 205
#define inf 0xfffffff

map<string,int>my;
int dis[maxn][maxn];
int Get_Len(char *a,char *b)
{
	int len=strlen(a),ans=0,i;
	for(i=0;i<len;i++)
		if(a[i]!=b[i])
			ans++;
	return ans>1?inf:1;  //注意
}

int main()
{
	char ss[205][15];
	char st[15],en[15];
	int n=0,i,j,k;
	for(i=0;i<maxn;i++)
		for(j=0;j<maxn;j++)
		{
			if(i==j)
				dis[i][j]=dis[j][i]=0;
			else
				dis[i][j]=dis[j][i]=inf;
		}
	my.clear();
	while(~scanf("%s",st))
	{
		if(st[0]!='*')
		{
			my[string(st)]=n;
			strcpy(ss[n],st);
			n++;
			for(i=0;i<n;i++)
				for(j=i+1;j<n;j++)
				{
					if(strlen(ss[i])!=strlen(ss[j]))
						continue;
					dis[i][j]=dis[j][i]=Get_Len(ss[i],ss[j]);
				}
			continue;
		}
		for(k=0;k<n;k++)
			for(i=0;i<n;i++)
				for(j=0;j<n;j++)
					if(dis[i][j]>dis[i][k]+dis[k][j])
						dis[i][j]=dis[i][k]+dis[k][j];
		int x,y;				
		while(~scanf("%s%s",st,en))
		{
			x=my[string(st)],y=my[string(en)];
			if(dis[x][y]<inf)
			{
				printf("%s %s %d\n",st,en,dis[x][y]);
				continue;
			}
			puts("Impossible");
		}
	}
	return 0;
}


提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值