05-图2. Saving James Bond - Easy Version (25)
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)的更多相关文章
- pat05-图2. Saving James Bond - Easy Version (25)
05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- 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 ...
- 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 ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- 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 ...
- PAT Saving James Bond - Easy Version
Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Delphi 动态改变Rzsplitter的Orientation(方向)属性
效果图: 原先不知道,弄了半天都改不了RzSplitter.Orientation = orHorizontal / orVertical 然后去查该组件的源代码,原来Orientation不是在Rz ...
- 34.Spring-Aop.md
http://blog.csdn.net/bigtree_3721/article/details/50759843 目录 [toc] --- 1.概念 1.1概念 AOP是Spring提供的关键特性 ...
- 重新开始学习javase_对象的摧毁
一.概述(转:@深入理解Java虚拟机:JVM高级特性与最佳实践(最新第二版) ) 经过半个世纪的发展,内存的动态分配与内存回收技术已经相当成熟,一切看起来都进入了“自动化”时代,那为什么我们还要去了 ...
- WPS 去掉自动打开的文档漫游和在线模板
关闭文档漫游 在cmd(命令提示符)中输入regedit.exe回车,将弹出”注册表编辑器“,选择HKEY_CURRENT_USER>>Software>>Kingsoft& ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- Kafka笔记--指定消息的partition规则
参数的设定:参考资料 不错的资料:http://blog.csdn.net/honglei915/article/details/37697655 http://developer.51cto.com ...
- Stack集合 Queue队列集合 Hashtable哈希表
Stack集合 干草堆集合 栈集合 栈;stack,先进后出,一个一个赋值,一个一个取值,安装顺序来. 属性和方法 实例化 初始化 Stack st = new Stack(); 添加元素 个数 Co ...
- iPhone 被同步到 Mac上后 如果不希望更新到Mac上在哪里删除?
前往文件夹 /Users/用户名/Library/Application Support/MobileSync 直接删除 就行了(同时要倾倒废纸篓). 目前iPhone链接Mac 后 不让 ...
- 自然语言处理(5)之Levenshtein最小编辑距离算法
自然语言处理(5)之Levenshtein最小编辑距离算法 题记:之前在公司使用Levenshtein最小编辑距离算法来实现相似车牌的计算的特性开发,正好本节来总结下Levenshtein最小编辑距离 ...
- 转:使用XHProf优化PHP程序
原文来自于:http://blog.sina.com.cn/s/blog_665fc8980100l8dq.html XHProf 是 FaceBook 开发的一个函数级别的 PHP 分层分析器. 数 ...