您的位置:首页 > 其它

单词游戏

2011-10-18 16:28 190 查看
单词游戏

源程序名 words.???(pas, c, cpp)

可执行文件名 words.exe

输入文件名 words.in

输出文件名 words.out

【问题描述】

Io和Ao在玩一个单词游戏。

他们轮流说出一个仅包含元音字母的单词,并且后一个单词的第一个字母必须与前一个单词的最后一个字母一致。

游戏可以从任何一个单词开始。

任何单词禁止说两遍,游戏中只能使用给定词典中含有的单词。

游戏的复杂度定义为游戏中所使用的单词长度总和。

编写程序,求出使用一本给定的词典来玩这个游戏所能达到的游戏最大可能复杂度。

【输入】

输入文件的第一行,表示一个自然数N(1≤N≤16),N表示一本字典中包含的单词数量以下的每一行包含字典中的一个单词,每一个单词是由字母A、E、I、O和U组成的一个字符串,每个单词的长度将小于等于100,所有的单词是不一样的。

【输出】

输出文件仅有一行,表示该游戏的最大可能复杂度。

【样例】

words.in words.out

5 16

IOO

IUUO

AI

OIOOI

AOOI

=============================

一道搜索:

不过要加2个剪枝才可全部过完

1.判断当前单词是否可以和其他单词组合

2.总长度是否达到最长,达到最长就退出。

-------------------------------------------------------

刚开始只想到了第2个剪枝.

其实现在想想要结合第一个剪枝第二个剪枝才能发挥最大的作用

不过还是有一些侥幸的思想在里面。

------------------------------------------------------------------------------------

正解应该为欧拉路..

================================================

type
node=record
h,t:char;
v:longint;
end;
var
n:longint;
nt:longint;
st:string;
word:array[1..16]of node;
//h,t:array[1..6]of longint;
ans:longint;
f:array[1..16]of boolean;
procedure init;
begin
assign(input,'words.in');
assign(output,'words.out');
reset(input); rewrite(output);
end;

procedure terminate;
begin
close(input); close(output);
halt;
end;

{function num(ch:char):longint;
begin
case ch of
'A': exit(1);
'E': exit(2);
'I': exit(3);
'O': exit(5);
'U': exit(6);
end;
end;    }

procedure dfs(t,tot:longint;ch:char);
var
i:longint;
begin
if t>nt then begin writeln(ans); terminate; end;
for i:=1 to n do
if (f[i])and ((ch='*')or(ch=word[i].h)) then
begin
f[i]:=false;
if ans<tot+word[i].v then ans:=tot+word[i].v;
dfs(t+1,tot+word[i].v,word[i].t);
f[i]:=true;
end;
end;

procedure main;
var
i,j:longint;

begin
readln(n);
fillchar(f,sizeof(f),false);
for i:=1 to n do
begin
readln(st);
word[i].h:=st[1];
word[i].t:=st[length(st)];
word[i].v:=length(st);
end;
for i:=1 to n do
for j:=1 to n do
if i<>j then
begin
if word[i].t=word[j].h then
begin
f[i]:=true;
f[j]:=true;
end;
end;
nt:=n;
ans:=0;
for i:=1 to n do
if not f[i] then
begin
dec(nt);
if ans<word[i].v then ans:=word[i].v;
end;

dfs(1,0,'*');
writeln(ans);
end;

begin
init;
main;
terminate;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: