1 边界和湖心小岛分别算一个节点。连接全部距离小于D的鳄鱼。时间复杂度O(N2)

2 推断每一个连通图的节点中是否包括边界和湖心小岛,是则Yes否则No

3 冗长混乱的函数參数

#include <stdio.h>
#include <malloc.h>
#include <queue>
#include <math.h> using namespace std; struct Coordinate
{
float x;
float y;
}; bool operator==(Coordinate& a, Coordinate& b)
{
return a.x == b.x && a.y == b.y;
} float DistanceOfPoints(const Coordinate& a, const Coordinate& b)
{
return sqrtf(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
} void JudgePosition(const int& D, Coordinate* crocodile, const int& i, bool* isCloseToEdge, bool* isCloseToCenter)
{
// 靠近湖岸
if (crocodile[i].x >= 50 - D || crocodile[i].x <= -50 + D ||
crocodile[i].y >= 50 - D || crocodile[i].y <= -50 + D)
{
isCloseToEdge[i] = true;
}
else
{
isCloseToEdge[i] = false;
}
// 靠近湖心小岛
if ( sqrtf(pow(crocodile[i].x, 2) + pow(crocodile[i].y, 2)) <= 7.5 + D)
{
isCloseToCenter[i] = true;
}
else
{
isCloseToCenter[i] = false;
}
} bool IsCloseToEdge(const int& D, const Coordinate& crocodile)
{
return (crocodile.x >= 50 - D || crocodile.x <= -50 + D ||
crocodile.y >= 50 - D || crocodile.y <= -50 + D);
} bool IsCloseToCenter(const int& D, const Coordinate& crocodile)
{
return (sqrtf(pow(crocodile.x, 2) + pow(crocodile.y, 2)) <= 7.5 + D);
} int* CreateMatrixGraph(const int& N)
{
int* graph = (int*) malloc(sizeof(int) * N * N);
for (int i = 0;i < N * N; i++)
{
graph[i] = 0;
}
return graph;
} bool IsMatrixConnected(const int& a, const int& b, int* graph, const int& N)
{
if (a == b)
{
return false;
}
return (graph[a * N + b]);
} void MatrixConnect(const int& a, const int& b, int* graph, const int& N)
{
if (IsMatrixConnected(a, b, graph, N))
{
printf("ERROR : %d AND %d ALREADY CONNECTED\n", a, b);
return;
}
if (a == b)
{
printf("ERROR : THE SAME VERTICE\n");
return;
}
graph[a * N + b] = 1;
graph[b * N + a] = 1;
} void GetAdjoinVertice(const int& vertice, int* graph, int* adjoinVertice, int N)
{
int currentIndex = 0;
for (int i = 0; i < N; i++)
{
if (graph[vertice * N + i] == 1)
{
adjoinVertice[currentIndex++] = i;
}
}
} void DFS(int* graph, const int& vertice, bool* isVisited, int N, bool* result)
{
//printf("%d ", vertice);
isVisited[vertice] = true;
if (vertice == N - 2)
{
result[0] = true;
}
if (vertice == N - 1)
{
result[1] = true;
} int* adjoinVertice = (int*) malloc(sizeof(int) * N);
for (int i = 0; i < N; i++)
{
adjoinVertice[i] = -1;
}
GetAdjoinVertice(vertice, graph, adjoinVertice, N); int i = 0;
while (adjoinVertice[i] != -1)
{
if (!isVisited[adjoinVertice[i]] /*&& DistanceOfPoints(crocodile[vertice], crocodile[i]) <= D*/)
{
DFS(graph, adjoinVertice[i], isVisited, N, result);
}
i++;
}
free(adjoinVertice);
} void BFS(int* graph, int vertice, bool* isVisited, int N)
{
queue<int> t;
t.push(vertice);
isVisited[vertice] = true; while (!t.empty())
{
int currentVertice = t.front();
t.pop();
printf("%d ", currentVertice); int* adjoinVertice = (int*) malloc(sizeof(int) * N);
for (int i = 0; i < N; i++)
{
adjoinVertice[i] = -1;
}
GetAdjoinVertice(currentVertice, graph, adjoinVertice, N);
int i = 0;
while (adjoinVertice[i] != -1)
{
if (!isVisited[adjoinVertice[i]])
{
t.push(adjoinVertice[i]);
isVisited[adjoinVertice[i]] = true;
}
i++;
}
}
} bool MatrixComponentsSearch(int* graph, bool* isVisited, int N, bool* result, int function = 1)
{
for (int i = 0; i < N; i++)
{
if (!isVisited[i])
{
if (function == 1)
{
//printf("{ ");
DFS(graph, i, isVisited, N, result);
if (result[0] == true && result[1] == true)
{
return true;
}
result[0] = false;
result[1] = false;
}
else
{
//printf("{ ");
BFS(graph, i, isVisited, N);
//printf("}\n");
}
}
}
return false;
} int main(void)
{
int N;
int D;
scanf("%d %d", &N, &D);
int nodeCount = N + 2;
Coordinate* crocodile = (Coordinate*) malloc(sizeof(Coordinate) * nodeCount);
bool* isVisited = (bool*) malloc(sizeof(bool) * N); for (int i = 0; i < N; i++)
{
scanf("%f %f", &crocodile[i].x, &crocodile[i].y); }
crocodile[N].x = 0;
crocodile[N].y = 0;
crocodile[N + 1].x = -1;
crocodile[N + 1].y = -1;
// 一共N个鳄鱼。N是湖心小岛。N+1是岸边
int* graph = CreateMatrixGraph(N + 2);
// 连接距离小于D的鳄鱼
for (int i = 0; i < N; i++)
{
if (IsCloseToCenter(D, crocodile[i]))
{
MatrixConnect(i, N, graph, nodeCount);
}
if (IsCloseToEdge(D, crocodile[i]))
{
MatrixConnect(i, N + 1, graph, nodeCount);
}
for (int j = i + 1; j < N; j++)
{
if (DistanceOfPoints(crocodile[i], crocodile[j]) <= D)
{
MatrixConnect(i, j, graph, nodeCount);
}
}
} bool result[2];
result[0] = false;
result[1] = false;
if (MatrixComponentsSearch(graph, isVisited, nodeCount, result))
{
printf("Yes");
}
else
{
printf("No");
} return 0;
}

05-图2. Saving James Bond - Easy Version (25)的更多相关文章

  1. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  2. PTA 06-图2 Saving James Bond - Easy Version (25分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  3. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  4. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

  5. Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33

    06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...

  6. PAT Saving James Bond - Easy Version

    Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...

  7. 06-图2 Saving James Bond - Easy Version

    题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625 This time let us consider the situation in ...

  8. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  9. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

随机推荐

  1. 使用Instant Client配置PL/SQL Developer

    之前使用PL/SQL Developer都是直接在本机安装完整版的Oracle Database,一是省事,二是可以在本机做一些demo测试:最近换了台电脑,感觉Instant Client更简单一些 ...

  2. Swift - 03 - 整数类型

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  3. 更改tabBarItem图片的问题

    代码: UIImage *normal = [[UIImage imageNamed:@"tabbar_home_default"] imageWithRenderingMode: ...

  4. 简单的js反选,全选,全不选

    <html> <head> <base href="<%=basePath%>"> <title>My JSP 'che ...

  5. 【USACO 3.1.5】联系

    [描述] 奶牛们开始对用射电望远镜扫描牧场外的宇宙感兴趣.最近,他们注意到了一种非常奇怪的脉冲调制微波从星系的中央发射出来.他们希望知道电波是否是被某些地外生命发射出来的,还是仅仅是普通的的星星发出的 ...

  6. 『重构--改善既有代码的设计』读书笔记----Remove Assignments to Parameters

    C++存在按值传递和按引用传递两种传递方式,Java严格按照按值传递这种方式来进行.以按值传递方式的角度来说,如果你 int test(int a) { ) { a = 1; } return a; ...

  7. 《JavaScript dom 编程艺术》 placeholder占位符IE8兼容办法。

    在<JavaScript dom 编程艺术>第11章学来的. 相对于用JavaScript替换文本框的提示语句 <!DOCTYPE html> <html lang=&q ...

  8. set_time_limit() 控制页面运行时间

    当你的页面有大量数据时,建议使用set_time_limit()来控制运行时间,默认是30s,所以需要你将执行时间加长点,如 set_time_limit(300)  ,其中将秒数设为0 ,表示持续运 ...

  9. 鼠标划过图片title 提示实现

    鼠标划过图片title 提示实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  10. C++explicit关键字

    在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: *     explicit  ...