HDU-1181 变形课
题意:给出n个单词,判断是否存在以‘b’开头,‘m’结尾的情况。
思路:dfs.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1181
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 const int N=10010; 10 11 string str[N]; 12 bool vis[N],flag; 13 int cnt; 14 15 void dfs(char c){ 16 if(c=='m'){flag=true;return ;} 17 for(int i=0;i<cnt;i++){ 18 if(!vis[i]&&str[i][0]==c){ 19 vis[i]=true; 20 dfs(str[i][str[i].length()-1]); 21 vis[i]=false; 22 } 23 } 24 } 25 26 int main(){ 27 28 // freopen("data.in","r",stdin); 29 // freopen("data.out","w",stdout); 30 31 cnt=0; 32 while(cin>>str[cnt++]){ 33 if(str[cnt-1]=="0"){ 34 memset(vis,false,sizeof(vis)); 35 flag=false; 36 dfs('b'); 37 if(flag) puts("Yes."); 38 else puts("No."); 39 cnt=0; 40 } 41 } 42 return 0; 43 }