SOJ 2325:Word Transformation _floyd

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 [email protected]. 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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章