【算法】两道经典地图算法题

第一题:点集数量非常大,里面可能存在两点之间距离小于d的状况,请写一个算法,将这些点找出来。

思路:建立矩阵空间索引,边界点标注,距离运算处理

1.首先建立距离为10d的网格空间索引,对每个点进行判断,若该点符合距离索引边界小于d的特征,则将其标记为边界点,存到特定数组中。

2.对每个索引网格内的点集进行距离运算,若距离小于d,则标记为错误点。

3.对于确定的边界点集,将相邻网格空间的边界点进行运算距离,若距离小于d,则标记为错误点。

Tips :在计算距离时,不要直接计算,先判读x坐标的之差与y坐标之差的绝对值,若二者有其一是大于d的,则无需继续计算下去,若均小于d,则只计算平方值是否小于d平方即可。


第二题:已知一个pointcurrent,求出离该点最近的n个点,并将其删除。

思路:空间索引,外接矩形

利用上一步建立好的空间索引,对pointcurrent所在的网格内的点进行统计,若小于n,则统计相邻网格的点数,若小于n,则继续此操作,若大于n,则建立相邻网格的点集,将点集中的点于pointcurrent 做距离计算,并排序。



第一题拓展:求点集中,距离最近的两个点。

思路:分治法

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
算法sicily例 1000. sicily 1155. Can I Post the lette Time Limit: 1sec Memory Limit:32MB Description I am a traveler. I want to post a letter to Merlin. But because there are so many roads I can walk through, and maybe I can’t go to Merlin’s house following these roads, I must judge whether I can post the letter to Merlin before starting my travel. Suppose the cities are numbered from 0 to N-1, I am at city 0, and Merlin is at city N-1. And there are M roads I can walk through, each of which connects two cities. Please note that each road is direct, i.e. a road from A to B does not indicate a road from B to A. Please help me to find out whether I could go to Merlin’s house or not. Input There are multiple input cases. For one case, first are two lines of two integers N and M, (N<=200, M<=N*N/2), that means the number of citys and the number of roads. And Merlin stands at city N-1. After that, there are M lines. Each line contains two integers i and j, what means that there is a road from city i to city j. The input is terminated by N=0. Output For each test case, if I can post the letter print “I can post the letter” in one line, otherwise print “I can't post the letter”. Sample Input 3 2 0 1 1 2 3 1 0 1 0 Sample Output I can post the letter I can't post the letter Source Code #include <iostream> #include <vector> using namespace std; int n,m; vector<int> vout[200]; bool visited[200]; bool flood(int u) { visited[u]=1; if (u==n-1) return 1; for (int x=0; x<vout[u].size(); x++) { int &v=vout[u][x]; if (!visited[v] && flood(v)) return 1; } return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值