换了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;
}