您的位置:首页 > 其它

HDOJ1181变形记 DFS

2016-06-11 13:00 281 查看
这操蛋的输入格式。。。。貌似只要过第一组数据就行了。多组测试我答案是有错的。

这个代码可以ac。。。

#include <iostream>
#include"sstream"
#include"string"
#include"algorithm"
#include"cstring"
#include"cstdio"
using namespace std;
struct se
{
char s;
char e;
se(){}
}str[1000];
int n;
int escape;
int visit[1000];
void dfs(char c)
{
int i;
if(escape) return ;
if(c=='m') {escape=1;return;}
for(i=0;i<n;i++)
if(str[i].s==c&&!visit[i])
{
visit[i]=1;
dfs(str[i].e);
}
}
int main()
{
memset(visit,0,sizeof(visit));
string a;
int i=0;
while(cin>>a)
{
if(a!="0")
{
str[i].s=a[0];
str[i].e=a[a.size()-1];
i++;
}
else
{
n=i;
escape=0;
dfs('b');
if(escape) cout<<"Yes."<<endl;
else cout<<"No."<<endl;
}
}

return 0;
}

这个是改完的

#include <iostream>
#include"sstream"
#include"string"
#include"algorithm"
#include"cstring"
#include"cstdio"
using namespace std;
struct se
{
char s;
char e;
se(){}
}str[1000];
int n;
int escape;
int visit[1000];
void dfs(char c)
{
int i;
if(escape) return ;
if(c=='m') {escape=1;return;}
for(i=0;i<n;i++)
if(str[i].s==c&&!visit[i])
{
visit[i]=1;
dfs(str[i].e);
}
}
int main()
{
string a;
int i=0;
while(cin>>a)
{

if(a!="0")
{
str[i].s=a[0];
str[i].e=a[a.size()-1];
i++;
memset(visit,0,sizeof(0));
}
else
{
n=i;
escape=0;
dfs('b');
if(escape) cout<<"Yes."<<endl;
else cout<<"No."<<endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: