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 ...
随机推荐
- UITableView中的visibleCells的用法(visibleCells帮上大忙了)
这两天遇到一个问题,UITableView中需要加入动画,而且每一行的速度不一样. 刚开始做时把所有的cell都遍历一遍加上动画,后来发现,如果数据很多时,就会出现各种各样的问题,而且没有显示在界 ...
- JS屏蔽右键菜单,复制,粘帖xxxxx........
//屏蔽右键菜单 document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try ...
- js 实现win7任务栏拖动效果
前言 在某个时刻, 我认识了一个朋友. 此人在我的教唆下, 踏上了js的不归路. 前天他问我, Win7任务栏拖动效果怎么实现. 我随口就跟他说, 这简单的一逼. 在我一晚上的折腾之后, 一份潦草的代 ...
- hdu 4631(最近点对,容器)
点击打开链接 题意: 给你一个平面,每次加入一个点,当点数>=2时,求最近点对距离的平方,最后输出所有的平方和. 给你a,b,c x[0]=0;x[i]=(x[i-1]*a+b)%c 如果按照平 ...
- android布局1
第二类:属性值必须为id的引用名“@id/id-name” 仅RelativeLayout中有效 android:layout_below 在某元素的下方 android:la ...
- js学习笔记之:数组(二)
今天来学习一下数组的遍历.删除等知识点: 1 数组的遍历 数组元素的遍历可以使用for循环,采用关键字for...in var aCity = new Array("北京" ...
- CSS布局:div高度随窗口变化而变化(BUG会有滚动条)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- css 日常
去掉input边框 outline:none; 不让用户选择文本 user-select: none; 手机网页点击输入框的瞬间会出现灰色背景 解决方案: -webkit-tap-high ...
- jdbc 连接mysql Communications link failure的解决办法
使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...
- Angular2案例rebirth开源
Angular2案例rebirth开源 在过去的几年时间里,Angular1.x显然是非常成功的.但由于最初的架构设计和Web标准的快速发展,逐渐的显现出它的滞后和不适应.这些问题包括性能瓶颈.滞后于 ...