/**
* 拯救007
*/
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define YES 101
#define NO -101//防止输入的坐标与函数circle()返回值相同
#define OK 1
#define max 100
typedef int answer;
typedef struct{//坐标
int x;//顶点
int y;
}VertexType;
typedef struct ArcNode{//边节点
int adjvex;//该边指向顶点的位置
struct ArcNode *nextarc;//指向下一条边的指针
}ArcNode;
typedef struct VNode{//坐标
int number;//第number个顶点
VertexType data;
ArcNode *firstarc;//指向第一条依附于该顶点的边的指针
}VNode,AdjList[max];//邻接表顶点数组
typedef struct{
AdjList vertices;
int vexnum;
int jumpDistance;
}ALGraph;
bool visited[max] = {false};
//第一个圆:以小岛和007跳跃为半径画圆找第一条鳄鱼并连上边
int circle1(ALGraph &G){
//设置原点
G.vertices[0].data.x = 0;
G.vertices[0].data.y = 0;
G.vertices[0].firstarc = NULL;
G.vertices[0].number = 0;
visited[0]=true;
double range = G.jumpDistance + 7.5;//得到小岛半径和007跳跃距离的总和
for(int i=1;i<G.vexnum+1;i++){
double x = pow(abs(G.vertices[i].data.x),2);
double y = pow(abs(G.vertices[i].data.y),2);
double result = sqrt(x+y);//得到鳄鱼的半径距离
//cout<<"第"<<i<<"条鳄鱼的半径为"<<result<<endl;
if(visited[G.vertices[i].number]!=true&&result<=range){
//将边连接到顶点的后面,建立两个点之间的联系
ArcNode* p1 = new ArcNode;
p1->adjvex = i;
p1->nextarc = G.vertices[0].firstarc;
G.vertices[0].firstarc = p1;
ArcNode* p2 = new ArcNode;
p2->adjvex = 0;
p2->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p2;
visited[G.vertices[i].number]=true;
return i;//返回第i条鳄鱼
}
}
return NO;
}
//创建邻接表的顶点
int CreateUDG(ALGraph &G){
cout<<"输入鳄鱼数量和007跳跃的最远距离"<<endl;
cin>>G.vexnum>>G.jumpDistance;
for(int i=1;i<G.vexnum+1;i++){
cin>>G.vertices[i].data.x;
cin>>G.vertices[i].data.y;
G.vertices[i].firstarc=NULL;
G.vertices[i].number = i;
}
return OK;
}
//判断当前顶点能否到岸
int isSafe(ALGraph G,int number){
if((abs(G.vertices[number].data.x)+G.jumpDistance)>=50||(abs(G.vertices[number].data.y)+G.jumpDistance)>=50){
return YES;
}else{
return NO;
}
return OK;
}
//遍历邻接表可连接的顶点并生成边
int DFS(ALGraph &G,int nowNode){
for(int i=1;i<G.vexnum+1;i++){
if(visited[i]!=true){
int x = int(pow(abs(G.vertices[i].data.x-G.vertices[nowNode].data.x),2));//这两行得根据符号相加减
int y = int(pow(abs(G.vertices[i].data.y-G.vertices[nowNode].data.y),2));//
int result = int(sqrt(x+y));//得到鳄鱼的半径距离
if(result<=G.jumpDistance){
//将边连接到顶点的后面,建立两个点之间的联系
ArcNode* p1 = new ArcNode;
p1->adjvex = i;
p1->nextarc = G.vertices[nowNode].firstarc;
G.vertices[nowNode].firstarc = p1;
ArcNode* p2 = new ArcNode;
p2->adjvex = nowNode;
p2->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p2;
visited[i]=true;
int DFS_result = DFS(G,i);
if(DFS_result==YES){
return DFS_result;
}
}
}
}
// cout<<"遍历"<<endl;
if(isSafe(G,nowNode)==YES){
return YES;
// cout<<"YES"<<endl;
// exit(0);
}
// cout<<"遍历"<<endl;
return NO;
}
int main(){
ALGraph G;
CreateUDG(G);
int firstNode = circle1(G);//这里可能有两个以上的点在小岛和007跳跃范围内
//cout<<firstNode<<endl;
if(firstNode == NO){
cout<<"NO"<<endl;
cout<<"007必死"<<endl;
exit(0);
}
int result = DFS(G,firstNode);
if(result == YES){
cout<<"YES"<<endl;
}
if(result == NO){
cout<<"NO"<<endl;
cout<<"007必死"<<endl;
}
return OK;
}
测试结果如下:
以上便是测试,这代码还能优化,等有空优化完再上传一次