zoukankan      html  css  js  c++  java
  • HDU 变形课 (dfs)

    题面

    Problem Description
    呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体.
    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.

    [hint]Hint[/hint]
    Harry 可以念这个咒语:"big-got-them".

    思路

    网上看了题解据说有四种姿势?深搜广搜,并查集和弗洛伊德传递闭包。刚上来我是想用并查集做的,but不会写....太菜了。深搜的话比较好想,我们找到以b开头的那个字串去查看尾字母是不是m,不是的话就把这个尾字母当成下一个要搜索的首字母再去搜,同时因为首字母相同的字串有多个,所以我们都要一一搜索,这个过程不要忘记回溯。还有这题的输入可能会比较恶心。

    代码实现

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <list>
    #include <map>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <numeric>
    #include<vector>
    #include <functional>
    using namespace std;
    typedef long long ll;
    const int N=100000+10;
    int vis[1010];
    char a[1010][2];
    int cnt;
    bool dfs (char z) {
       for (int i=0;i<cnt;i++) {
          if (a[i][0]==z&&!vis[i]) {
             vis[i]=1;
             if (a[i][1]=='m') return true;
             if (dfs (a[i][1])) return true;
             vis[i]=0;
          }
       }
       return false;
    }
    int main() {
        char s[1010];
        cnt=0;
        while (cin>>s) {
           if (s[0]=='0') {
              memset (vis,0,sizeof (vis));
              cout<<dfs ('b')<<endl;
              if (dfs ('b')) printf ("Yes.
    ");
              else printf ("No.
    ");
              cnt=0;
              continue;
           }
           a[cnt][0]=s[0];
           int len=strlen(s);
           a[cnt++][1]=s[len-1];
        } 
        return 0;   
    }
    
    
    
  • 相关阅读:
    从dotNet到VB6之模仿构造OleDbDataAdapter与dataset结合
    编程的偷懒艺术与美感
    给您参考,现在开发数据库项目用.net 2005成熟吗?还是用.net2003比较有保证
    access数据库版权及容量问题
    中天股票数据格式
    受伤与药油的最佳搭配
    关于发布各种股票软件数据格式
    .net 与flash8传递(互传)数组的技巧
    我的C语言合集
    ZOJ 1205 Martian Addition 解题报告
  • 原文地址:https://www.cnblogs.com/hhlya/p/13238048.html
Copyright © 2011-2022 走看看