05-2

换了vs

终于大致知道怎么用了,想念cfree啊

进入到了图,开始感觉简单到爆,事实证明我错了

图,先考虑用什么数据结构,

g[max][max]

还有一种用vector容器的,后面有习题

05-2 有一个测试点没过

http://www.patest.cn/contests/mooc-ds/05-2

我的错误主要是

1.对dfs不熟悉

void dfs(int x){
	visit[x]=1;
	for(g中x的邻接点v){
		if(visit[v]==0){
			dfs(v);
		}	
	}
}
void all(){
	for(所有的节点x){
		if(visit[x]==0){
			dfs(x);
		}
	}
}
2.递归返回

我写了2种递归方法2错误,错在了递归返回时,不能正确的返回到调用的地方

方法1

bool dfs(int i,int n,double d){
	bool ans=0;
	po[i].flag=1;
	double x0=po[i].x;
	double y0=po[i].y;
	if(issafe(x0,y0,d)){
		ans=1;
	}else{
		for(int j=0;j<n;j++){
			if(po[j].flag==0 && isreach(j,d,x0,y0)==1){
				ans=dfs(j,n,d);
				if(ans==1){
					break;
				}
			}
		}
	}

	return ans;
}

方法2
bool dfs(int i,int n,double d,double x0,double y0){
    bool answer=0;
    po[i].flag=1;
    if(issafe(x0,y0,d)==1){
        answer=1;
    }else{
        for(int j=0;j<n;j++){

            if(po[j].flag==0&&isreach(j,d,x0,y0)){
                x0=po[j].x;
                y0=po[j].y;
                answer=dfs(j,n,d,x0,y0);
                if(answer==1){
                    break;
                }
            }
        }
    }
    return answer;
}

比较

方法1


方法2

3.对于第一次需要单独判断


#include <stdio.h>
#include <math.h>
#define  MAX 110
struct point{
	double x;
	double y;
	bool flag;
}po[MAX];
void init(){
	for(int i=0;i<MAX;i++){
		po[i].x=0;
		po[i].y=0;
		po[i].flag=0;
	}
}
bool isreach(int i,double d,double x0,double y0){
	double x=po[i].x;
	double y=po[i].y;
	double tmp=(x0-x)*(x0-x)+(y0-y)*(y0-y);
	if(x0==0&&y0==0){
		d+=7.5;  
	}
	tmp=sqrt(tmp);
	if(d>=tmp){
		return true;
	}else{
		return false;
	}
}

bool issafe(double x0,double y0,double d){
	//double x0=po[i].x;
	//double y0=po[i].y;
	if(x0==0&&y0==0){
		d+=7.5;
	}
	if(x0+d<=50 && x0-d>=-50 && y0+d<=50 && y0-d>=-50){
		return false;
	}else{
		return true;
	}
}
bool dfs(int i,int n,double d){
	bool ans=0;
	po[i].flag=1;
	double x0=po[i].x;
	double y0=po[i].y;
	if(issafe(x0,y0,d)){
		ans=1;
	}else{
		for(int j=0;j<n;j++){
			if(po[j].flag==0 && isreach(j,d,x0,y0)==1){
				ans=dfs(j,n,d);
				if(ans==1){
					break;
				}
			}
		}
	}

	return ans;
}
//bool dfs(int i,int n,double d,double x0,double y0){
//	bool answer=0;
//	po[i].flag=1;
//	if(issafe(x0,y0,d)==1){
//		answer=1;
//	}else{
//		for(int j=0;j<n;j++){
//
//			if(po[j].flag==0&&isreach(j,d,x0,y0)){
//				x0=po[j].x;
//				y0=po[j].y;
//				answer=dfs(j,n,d,x0,y0);
//				if(answer==1){
//					break;
//				}
//			}
//		}
//	}
//	return answer;
//}
int main(){

	freopen("in.txt","r",stdin);
	int n,d;
	while(scanf("%d %d",&n,&d)!=EOF){
		init();
		for(int i=0;i<n;i++){
			scanf("%lf %lf",&po[i].x,&po[i].y);
		}
		double x0=0;
		double y0=0;
		bool ans=0;
		if(issafe(x0,y0,d)){
			ans=1;
		}else{
			for(int i=0;i<n;i++){
				if(po[i].flag==0&&isreach(i,d,x0,y0)==1){
					ans=dfs(i,n,d);
					/*x0=po[i].x;
					y0=po[i].y;
					ans=dfs(i,n,d,x0,y0);*/
					if (ans==1)
					{
						break;
					}
				}
			}

		}

		if(ans){
			printf("Yes\n");
		}else{
			printf("No\n");
		}


	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值