HDOJ 1181 HDU 1181 變形課 ACM 1181 IN HDU
Posted on 2010-08-25 11:27 MiYu 閱讀(472) 評論(0) 編輯 收藏 引用 所屬分類: ACM ( 搜索 )MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋
題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1181
題目描述:
代碼變形課
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2655 Accepted Submission(s): 863
Problem Description
呃......變形課上Harry碰到了一點(diǎn)小麻煩,因?yàn)樗⒉幌馠ermione那樣能夠記住所有的咒語而隨意的將一個棒球變成刺猬什么的,但是他發(fā)現(xiàn)了變形咒語的一個統(tǒng)一規(guī)律:如果咒語是以a開頭b結(jié)尾的一個單詞,那么它的作用就恰好是使A物體變成B物體.
Harry已經(jīng)將他所會的所有咒語都列成了一個表,他想讓你幫忙計(jì)算一下他是否能完成老師的作業(yè),將一個B(ball)變成一個M(Mouse),你知道,如果他自己不能完成的話,他就只好向Hermione請教,并且被迫聽一大堆好好學(xué)習(xí)的道理.
Input
測試數(shù)據(jù)有多組。每組有多行,每行一個單詞,僅包括小寫字母,是Harry所會的所有咒語.數(shù)字0表示一組輸入結(jié)束.
Output
如果Harry可以完成他的作業(yè),就輸出"Yes.",否則就輸出"No."(不要忽略了句號)
Sample Input
so
soon
river
goes
them
got
moon
begin
big
0
Sample Output
Yes.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2655 Accepted Submission(s): 863
Problem Description
呃......變形課上Harry碰到了一點(diǎn)小麻煩,因?yàn)樗⒉幌馠ermione那樣能夠記住所有的咒語而隨意的將一個棒球變成刺猬什么的,但是他發(fā)現(xiàn)了變形咒語的一個統(tǒng)一規(guī)律:如果咒語是以a開頭b結(jié)尾的一個單詞,那么它的作用就恰好是使A物體變成B物體.
Harry已經(jīng)將他所會的所有咒語都列成了一個表,他想讓你幫忙計(jì)算一下他是否能完成老師的作業(yè),將一個B(ball)變成一個M(Mouse),你知道,如果他自己不能完成的話,他就只好向Hermione請教,并且被迫聽一大堆好好學(xué)習(xí)的道理.
Input
測試數(shù)據(jù)有多組。每組有多行,每行一個單詞,僅包括小寫字母,是Harry所會的所有咒語.數(shù)字0表示一組輸入結(jié)束.
Output
如果Harry可以完成他的作業(yè),就輸出"Yes.",否則就輸出"No."(不要忽略了句號)
Sample Input
so
soon
river
goes
them
got
moon
begin
big
0
Sample Output
Yes.
題目分析:
此題是一個很標(biāo)準(zhǔn)了 搜索題, 直接枚舉 + 回溯 就 OK了 .
代碼/*
MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋
http://www.shnenglu.com/MiYu
Author By : MiYu
Test :
Program :
*/
#include<iostream>
#include<string>
using namespace std;
struct{
char beg;
char end;
}M[101];
bool hash[101],f;
int N;
bool DFS ( char ch )
{
if ( f )
return true;
if( ch == 'm' )
{
f = true;
return true;
}
for ( int i = 0; i < N; ++ i )
if ( M[i].beg == ch && !hash[i] )
{
hash[i] = true;
DFS ( M[i].end );
hash[i] = false;
}
return false;
}
int main ()
{
string str;
while ( cin >> str )
{
N = 0;
f = false;
memset ( hash, 0 , sizeof ( hash ) );
while ( str != "0" )
{
M[N].beg = str[0];
M[N].end = str[ str.size() - 1 ];
N++;
cin >> str;
}
DFS ( 'b' );
puts ( f ? "Yes." : "No." );
}
return 0;
}
MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋
http://www.shnenglu.com/MiYu
Author By : MiYu
Test :
Program :
*/
#include<iostream>
#include<string>
using namespace std;
struct{
char beg;
char end;
}M[101];
bool hash[101],f;
int N;
bool DFS ( char ch )
{
if ( f )
return true;
if( ch == 'm' )
{
f = true;
return true;
}
for ( int i = 0; i < N; ++ i )
if ( M[i].beg == ch && !hash[i] )
{
hash[i] = true;
DFS ( M[i].end );
hash[i] = false;
}
return false;
}
int main ()
{
string str;
while ( cin >> str )
{
N = 0;
f = false;
memset ( hash, 0 , sizeof ( hash ) );
while ( str != "0" )
{
M[N].beg = str[0];
M[N].end = str[ str.size() - 1 ];
N++;
cin >> str;
}
DFS ( 'b' );
puts ( f ? "Yes." : "No." );
}
return 0;
}
代碼其實(shí)這題還有一種很 YD 的解法!!! 嘿嘿 ................
具體情況看代碼:
#include<iostream>
using namespace std;
char ss[10];
int main(){
int flag=1;
while(gets(ss)){
if (strcmp(ss,"0")==0){
if (flag){
printf("Yes.\n");
flag=0;
}
else
printf("No.\n");
}
}
return 0;
}
具體情況看代碼:
#include<iostream>
using namespace std;
char ss[10];
int main(){
int flag=1;
while(gets(ss)){
if (strcmp(ss,"0")==0){
if (flag){
printf("Yes.\n");
flag=0;
}
else
printf("No.\n");
}
}
return 0;
}
MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自
______________白白の屋


