新聞| | PChome| 登入
2011-12-10 07:25:39| 人氣1,853| 回應8 | 上一篇 | 下一篇

[UVA] 429 - Word Transformation

推薦 0 收藏 0 轉貼0 訂閱站台

 Word Transformation 

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.

Input and Output

The first line of the input is an integer N, indicating the number of test set that your correct program should test followed by a blank line. Each test set will have 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 alphabetic and in lower case, and no word will be longer than ten characters. 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 in a transformation. All pairs are guaranteed to have a transformation using the dictionary given. The starting and ending words will appear in the dictionary.

Two consecutive input set will separated by a blank line.

The output should contain one line per word pair for each test set, and must include the starting word, the ending word, and the number of steps in the shortest possible transformation, separated by single spaces. Two consecutive output set will be separated by a blank line.

Sample Input

1

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



作法 : 判斷任兩字串的轉換最短路徑長, 以下用 BFS
#include<stdio.h>
#include<string.h>
int main() {
char s[101], sx[101], sy[101], D[201][20];
int map[201][201], mt[201], size;
int n, i, j, k, fx, fy;
scanf("%d", &n);
getchar();
gets(s);
while(n--) {
size = 0;
while(gets(s)) {
if(s[0] == '*') break;
strcpy(D[size], s);
size++;
}
memset(mt, 0, sizeof(mt));
for(i = 0; i < size; i++) {
for(j = 0; j < size; j++) {
int dif = 0;
for(k = 0; D[i][k] && D[j][k]; k++)
if(D[i][k] != D[j][k])
dif++;
if(D[i][k] == 0 && D[j][k] == 0 && dif == 1)
map[i][mt[i]++] = j, map[j][mt[j]++] = i;
}
}
while(gets(s)) {
if(s[0] == '\0') break;
sscanf(s, "%s %s", sx, sy);
for(i = 0; i < size; i++)
if(!strcmp(sx, D[i])) {
fx = i;break;
}
for(i = 0; i < size; i++)
if(!strcmp(sy, D[i])) {
fy = i;break;
}
printf("%s %s ", sx, sy);
if(fx == fy) puts("0");
else {
int Used[size], Q[size*size], Qt = -1, V[size], tn;
memset(Used, 0, sizeof(Used));
Q[++Qt] = fx, V[fx] = 0, Used[fx] = 1;
for(i = 0; i <= Qt; i++) {
tn = Q[i];
if(tn == fy) break;
for(j = 0; j < mt[tn]; j++)
if(Used[map[tn][j]] == 0) {
Used[map[tn][j]] = 1, V[map[tn][j]] = V[tn]+1;
Q[++Qt] = map[tn][j];
}
}
printf("%d\n", V[fy]);
}
}
if(n) puts("");
}
return 0;
}

台長: Morris
人氣(1,853) | 回應(8)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11332 - Summing Digits
此分類上一篇:[UVA] 280 - Vertex

seo
The software says stunning to read these sort of revealing and additionally completely unique articles or reviews upon your webpages. online dispensary detroit http://www.riponpress.com/best-online-dispensary-buy-premium-weed-on-a-budget/article_335d4eac-ef16-11ef-9382-c7e603ecfd8d.html
2025-06-02 19:08:53
seo
Sustain the nice do the job, When i understand several threads within this web page in addition to I'm sure that a world-wide-web blog site is usually authentic useful possesses bought communities connected with excellent facts. IPTV UK http://xtremehd-uk.uk/
2025-07-09 19:07:01
seo
You could be allowed to put up manufacturers, except for shortcuts, in the event that they've been recognized not to mention concerning content. 【エロ漫画】童貞マチアプ体験記 http://erokamikagura.com/24418/
2025-07-21 21:22:41
seo
You could be allowed to put up manufacturers, except for shortcuts, in the event that they've been recognized not to mention concerning content. halloween lingerie for women http://www.amazon.com/dp/B09Z47LFQW
2025-07-22 17:44:08
seo
You could be allowed to put up manufacturers, except for shortcuts, in the event that they've been recognized not to mention concerning content. magnumslot http://magnumini.com/
2025-07-23 19:28:21
seo
Definitely As i browse the application last week though My partner and i various brain to sort it out and from now on Need be to enjoy a book the application repeatedly considering it is especially well crafted. https://www.reddit.com/r/SaaS/comments/1ljz2vh/comment/mzv7k9r/?context=3 http://www.reddit.com/r/SaaS/comments/1ljz2vh/comment/mzv7k9r/?context=3
2025-07-23 21:01:25
seo
Definitely As i browse the application last week though My partner and i various brain to sort it out and from now on Need be to enjoy a book the application repeatedly considering it is especially well crafted. bride thongs http://www.amazon.com/dp/B0C5ZYFP7Z
2025-07-24 19:39:54
seo
A totally free tell you which usually they can be a a superb content from the awesome people, we're very happy to watch this approach. Best B2B Travel Agency in Delhi http://blogs.lordstravelsonline.com/best-b2b-travel-agency-in-delhi/
2025-07-29 21:53:35
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文