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 ...
随机推荐
- Objective-C学习篇05—Foundation框架简介
iOS中所谓的框架,说到底就是一个目录,iOS提供了很多我们可以在应用程序中调用的框架.许多应用程序都使用了如Foundation.UIKit和Core Graphics这些框架.根据你为应用程序选择 ...
- eclipse/myeclipse 变量名自动补全问题
原理是:在输入变量名后,去掉按下空格或=后,代码上屏 以前只知道alt+/调出assist,后来发现可以所有字母都激活content assist(8.1里有写).用起来果然很爽,但是eclipse ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第三章:搜索、高级过滤和视图模型
在这一章中,我们首先添加一个搜索产品的模块以增强站点的功能,然后使用视图模型而不是ViewBag向视图传递复杂数据. 注意:如果你想按照本章的代码编写示例,你必须完成第二章或者直接从www.apres ...
- macbook Android开发环境搭建,真机调试
买了一台MacBook,本以为可以鼓捣一下iOS开发之类的,可惜导师要我做Android开发.无奈开始了在MacBook上开发Android的工作. 从开始配置环境到应用成功在真机上运行,也是曲曲折折 ...
- 分片传输——send和recv函数
最近在写socket编程收发数据,对于如何发送和接收大量数据,一直在思考.send和recv一般缓存区大小为4K,但是如果你要传输的数据超过了这个标准该如何做呢. 我想到的就是如改写write和rea ...
- 跟我一起学CMake
如今CMake使用的人数越来越多,包括我项目组里,很多大牛们在写Qt程序的时候都不用自带的qmake,貌似会出现很多问题,他们往往都用自己写的CMake来编译系统,今天我也和大家一起来学学这个高大上的 ...
- mysql主从复制 (超简单) 转载
怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 ...
- net Core 使用MyCat分布式数据库,实现读写分离
net Core 使用MyCat分布式数据库,实现读写分离 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 MyCat2.0版本很快就发布了,关于MyCat的动态和一些问题, ...
- iOS App转让流程
说法一: (1)选择转让APP (2)进入转让界面 点击Continue进入下一步 (3)输入对方的APP ID和Team ID Apple ID 和 Team ID 可以在m ...
- JS学习之页面加载
1.window.opener.location.reload(); 意思是让打开的父窗口刷新.window.opener指的是本窗口的父窗口,window.opener.location.h ...