| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
???In Reply To:水题啊。。一个DFS BFS就OK了,附代码(差点把DFS打成DNF,最近痴迷那个) Posted by:yzhw at 2010-01-26 22:26:42 为什么要深度搜索啊??不可以直接bfs吗??
我的代码:#include<iostream>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
bool map[500][500];
bool v[500][500];
int main()
{
int in,t,i,j,n,et;
char line[10];
FILE *f,*ff;
f=freopen("1.txt","r",stdin);
ff=freopen("2.txt","w",stdout);
scanf("%d\n",&in);
for(t=0; t<in; ++t){
scanf("%d%d\n",&n,&et);
memset(map,0,sizeof(map));
memset(v,0,sizeof(v));
while(gets(line))
{
if(strlen(line) == 0)
break;
sscanf(line,"%d%d",&i,&j);
map[i][j] = 1;
}
queue<int> q;
if(map[0][et]==1)
{
fprintf(ff,"0\n\n");
continue;
}
for(i=1;i<n;i++)
{
if(map[0][i]==1&&!v[0][i])
{
q.push(i);
v[0][i]=1;
}
}
int t;
while(!q.empty())
{
t=q.front();
q.pop();
for(i=1;i<n;i++)
{
if(map[t][i]==1&&!v[t][i])
{
if(i==et)
{
goto XXX;
}
else{
q.push(i);
v[t][i]=1;
}
}
}
}
XXX:fprintf(ff,"Put guards in room %d.\n",t);
if(t)
fprintf(ff,"\n");
}
fclose(f);
fclose(ff);
return 0;
}
但不知道哪里有错误!测试结果是没有错。
若这样不行的话,请解释一下为什么先要DFS,谢谢了
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator