标签:dfs
so soon river goes them got moon begin big 0
Yes.Harry 可以念这个咒语:"big-got-them".HintHint
题解:回溯 DFS查找第一位和当前字符串最后一位相同的字符串 如果 找到最后一位是m的字符串 则Harry可以完成作业 如果DFS所有还是找不到则不能完成作业
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1011;
string str[maxn];
bool vis[maxn];
int cnt = 0;
bool flag;
void DFS(string ch);
int main(){
while(cin >> str[cnt++]){
flag = false;
memset(vis, false, sizeof(vis));
while(str[cnt-1] != "0"){ //输入数据
cin >> str[cnt++];
}
vis[0] = true; //标记
DFS("b"); //搜索
printf("%s\n", flag ? "Yes." : "No.");
cnt = 0;
}
return 0;
}
void DFS(string ch){
if(ch[ch.size()-1] == 'm'){ //如果可以则标记flag并跳出
flag = true;
return;
}
for(int i = 0; i < cnt-1; ++i){
if(str[i][0] == ch[ch.size()-1] && !vis[i]){ //如果匹配则搜索并回溯
vis[i] = true;
DFS(str[i]);
vis[i] = false;
}
}
return;
}标签:dfs
原文地址:http://blog.csdn.net/u012431590/article/details/46272773