From df54e6c505f3bc183138fed449a701cca224ba3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=94=A1=E4=B8=80=E7=A2=9F?= <12155495+seu-15713701591@user.noreply.gitee.com> Date: Mon, 8 May 2023 13:56:17 +0000 Subject: [PATCH] update problem3.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 小蔡一碟 <12155495+seu-15713701591@user.noreply.gitee.com> --- problem3.cpp | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/problem3.cpp b/problem3.cpp index eda06d2..28c27ea 100644 --- a/problem3.cpp +++ b/problem3.cpp @@ -1,19 +1,19 @@ #include -//ʹGBK +//使用GBK编码 -//㷨 +//随机算法求解最近点对问题 using namespace std; - //ඨ + //点类定义 struct Point{ double x, y; }; -//ζ +//正方形定义 struct Square { @@ -39,7 +39,7 @@ int HASH(int x, int y){ return x * 100 + y; } -//һ꣬ӳһhashֵ +//输入一个点的坐标,映射出一个hash值 double calculate_distance(int i, int j){ @@ -47,7 +47,7 @@ double calculate_distance(int i, int j){ return sqrt((points[i].x - points[j].x) * (points[i].x - points[j].x) + (points[i].y - points[j].y) * (points[i].y - points[j].y)); } -// +//计算两点间距离 void Into_Hash(int i, double r){ @@ -57,14 +57,14 @@ void Into_Hash(int i, double r){ squares[now_index].point_data = point_to_square[HASH( (int)(points[i].x/r), (int)(points[i].y/r) )]; - //һӳ + //将点和正方形网格做一个互相映射 point_to_square[HASH( (int)(points[i].x/r), (int)(points[i].y/r) )] = now_index ++; } -//hash +//更新hash表 void Renew_Hash(int n, double r){ now_index = 0; @@ -76,9 +76,9 @@ void Renew_Hash(int n, double r){ Into_Hash(i, r); } - +//添加一些东西 -//8Լ9 +//向8个方向以及自身这9个格子搜索 double Get_len_around(int p, double r){ int x = (int) (points[p].x / r), y = (int) (points[p].y / r), i, j; @@ -86,7 +86,7 @@ double Get_len_around(int p, double r){ double s = 100000.0; for(int k = 0; k < 9; k ++){ - //9(8ھԼ) + //遍历9个方向(8个邻居以及自身) i = x + direction[k][0], j = y + direction[k][1]; @@ -101,8 +101,8 @@ double Get_len_around(int p, double r){ } vector random_permut_1(int n) -//ϴƺһn飬Ԫء[0,n-1]Ȼٴ -//һsqrt(n)ȵΪ㷨ʼߴļ߶ +//洗牌函数,生成一个n个数的数组,数组元素∈[0,n-1]然后再打乱 +//返回一个sqrt(n)长度的随机数数组作为随机算法初始网格尺寸的计算尺度 { vector temp; @@ -132,7 +132,7 @@ double Get_Min_Distance(int n){ if (calculate_distance(initial_subset[i], initial_subset[j]) < r){ r = calculate_distance(initial_subset[i], initial_subset[j]); - //¾ + //更新距离 } } @@ -141,24 +141,24 @@ double Get_Min_Distance(int n){ double s; if(r < 1e-6 || n < 2) return 0; - //ֻһ㣬ߵһξС(޽ӽ0) + //只有一个点,或者第一次就碰巧命中了最小距离(无限接近0) Renew_Hash(1, r); - //rΪ׼ + //以r为基准构造正方形网格 for(int i = 2; i < n; i ++) if((s = Get_len_around(i, r)) < r){ - //iķ鷢һ̾ + //第i个点的方块发现了一个更短距离 r = s; - if(r < 1e-6) return 0;//ҵһӽ0ֱ̾ӷ + if(r < 1e-6) return 0;//找到一个接近0的最短距离直接返回 Renew_Hash(i, r); - //µĸ̾¹ϣ + //依据这个新的更短距离更新哈希表 } else @@ -173,7 +173,7 @@ int main(){ int n; - printf("ɻĸ1~100:\n"); + printf("请输入飞机的个数1~100:\n"); scanf("%d", &n); @@ -183,18 +183,18 @@ int main(){ points[i].x = rand() % 500; points[i].y = rand() % 500; - printf("%dܷɻ: (%f %f)\n", i, points[i].x, points[i].y); + printf("第%d架飞机的坐标: (%f %f)\n", i, points[i].x, points[i].y); } double ans = Get_Min_Distance(n); - printf("룺%.4lf\n", ans); + printf("最近距离:%.4lf\n", ans); if(ans < 100.0) - printf("Σմڣ"); + printf("有危险存在!"); else - printf("ȫ"); + printf("安全"); system("pause"); -- Gitee