变形课
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7904 Accepted Submission(s): 2954
Problem Description
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
Sample Input
so soon river goes them got moon begin big 0
Sample Output
Yes.Harry 可以念这个咒语:"big-got-them".HintHint
——————————————————————————————————————————————————
用广搜做的,由于用了delete作为函数名,与C++的delete的保留字冲突,需要用Gcc提交,G++可能有问题
/****************************
*Name:变形课.c
*Tags:ACM search BFS
*GCC可以通过,G++由于delete关键字问题CE
****************************/
#include <stdio.h>
#include <string.h>
struct node {
char f, e;
} queue[100];
void BFS();
int insert(struct node );
struct node delete();
int empty();
char s[100][2];
int n, f, r, use[100];
int main()
{
char str[50];
int i, j, l;
while(gets(str) != NULL) {
n = 0;
while(str[0] != '0') {
l = strlen(str);
s[n][0] = str[0];
s[n++][1] = str[l-1];
gets(str);
}
f = 0;
r = 0;
BFS();
}
return 0;
}
int insert(struct node x)
{
queue[r].f = x.f;
queue[r].e = x.e;
r++;
return 0;
}
struct node delete()
{
struct node x;
x.f = queue[f].f;
x.e = queue[f].e;
f++;
return x;
}
int empty()
{
if(r == f) {
return 1;
}
return 0;
}
void BFS()
{
int i, j, f;
struct node x, new;
f = 1;
for(i = 0; i < 100; i++) {
use[i] = 0;
}
for(i = 0; i < n && f; i++) {
if(s[i][0] == 'b') {
if(s[i][1] == 'm') {
f = 0;
printf("Yes.\n");
} else {
use[i] = 1;
x.f = s[i][0];
x.e = s[i][1];
insert(x);
}
}
}
while(!empty() && f) {
x = delete();
for(i = 0; i < n; i++) {
if(use[i]) {
continue;
}
if(s[i][0] == x.e) {
if(s[i][1] == 'm') {
f = 0;
printf("Yes.\n");
} else {
use[i] = 1;
new.f = s[i][0];
new.e = s[i][1];
insert(new);
}
}
}
}
if(f) {
printf("No.\n");
}
}