SOJ 2325:Word Transformation _floyd

来源:互联网 发布:java中类中的static 编辑:程序博客网 时间:2024/06/05 05:14

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 0xfffffffmap<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;elsedis[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;}


原创粉丝点击