06-图2 Saving James Bond - Easy Version

荒凉一梦 提交于 2020-01-28 23:26:54
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

#define MaxVertexNum 100    /* 最大顶点数设为100 */
typedef int Vertex;         /* 用顶点下标表示顶点,为整型 */
typedef int WeightType;        /* 边的权值设为整型 */
typedef float DataType; 
int visited[MaxVertexNum], D, answer = 0;
        
/* 图结点的定义 */
typedef struct GNode *MGraph;
struct GNode
{
    int Nv;  /* 顶点数 */
    DataType x[MaxVertexNum]; 
    DataType y[MaxVertexNum];
};

void init(MGraph G)
{
	for(int i=0; i<G->Nv; i++) visited[i] = 0;
}

MGraph BuildGraph();
void Save007( MGraph G );

int main()
{	
	MGraph G = BuildGraph();
	init(G);
	Save007(G);
	return 0;
}

MGraph CreateGraph( int VertexNum )
{ /* 初始化一个有VertexNum个顶点但没有边的图 */
    Vertex V, W;
    MGraph Graph;     
    Graph = (MGraph)malloc(sizeof(struct GNode)); /* 建立图 */
    Graph->Nv = VertexNum;           
    return Graph; 
}

float distance(float x1, float y1, float x2, float y2)
{
	return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
}
 
MGraph BuildGraph()
{
    MGraph Graph;
    Vertex V;
    int Nv, i;    
    scanf("%d%d", &Nv, &D);   /* 读入顶点个数及跳的最远距离 */
    Graph = CreateGraph(Nv); /* 初始化有Nv个顶点但没有边的图 */      
    for(i=0; i<Nv; i++)
    {
    	scanf("%f%f", &Graph->x[i], &Graph->y[i]);    	
	}
    return Graph;
}

bool FirstJump(float x, float y)
{
	return (D >= (distance(0, 0, x, y)-7.5));
}

bool Jump(float x1, float y1, float x2, float y2)
{
	return (D >= distance(x1, y1, x2, y2));
}

bool IsSafe(float x, float y)
{
	return ((abs(x-50)<=D)||(abs(y-50)<=D));
}

int DFS( MGraph G, Vertex V ) 
{ 
	visited[V] = 1; 	
	if( IsSafe(G->x[V], G->y[V]) ) answer = 1; 
	else
	{  
		for(Vertex W=0; W<G->Nv; W++) 
		{
			if( !visited[W] && Jump(G->x[V], G->y[V], G->x[W], G->y[W]) ) 
			{ 
				answer = DFS(G, W); 
				if(answer==1) break; 
			} 
		} 
		return answer; 
	} 
}

void Save007( MGraph G ) 
{ 
	for (Vertex V=0; V<G->Nv; V++) 
	{ 
		if(!visited[V] && FirstJump(G->x[V], G->y[V])) 
		{ 
			answer = DFS(G, V); 
			if(answer==1) break; 
		} 
	} 
	if(answer==1) printf("Yes"); 
	else printf("No"); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!