我需要我的代码帮助。我有R * S网格和N点。每一点都有一些人。我需要从所有输入的点中为人们找到的步数最少的网格。距离以曼哈顿度量标准计算。行从0到R-1编号,列从0到S-1编号。在第一行上,我有R,S和N,在N上,下一行是N个点的坐标和位于该点的人数。如果存在更多点且步骤总和相同,则只需写一个即可。该算法可以找到中位数,但答案可能不正确。请您给我一些算法,给我正确的答案? 输入示例:
5 5 3
1 1 4
4 3 3
2 4 1
输出示例:
2 2
我写了这段代码,但是没有用。你能帮我解决吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int cmp(const void *a, const void *b) {
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
}
int main(void) {
int N, n, i, count, rx, ry, R, S;
scanf("%d %d %d", &R, &S, &N);
int a[N][2];
int b[N][2];
long int sum = 0;
for (i = 0; i < N; i++) {
scanf("%d %d %d", &a[i][0], &b[i][0], &a[i][1]);
b[i][1] = a[i][1];
sum = sum + a[i][1];
}
n = sizeof a / sizeof *a;
qsort(a, n, sizeof *a, cmp);
if (sum % 2 == 1) {
count = (sum + 1) / 2;
for (i = 0; i < N; i++) {
count = count - a[i][1];
if (count == 0) {
rx = a[i][0];
break;
}
if (count < 0) {
rx = a[i-1][0];
break;
}
}
}
if (sum % 2 == 0) {
count = sum / 2;
for (i = 0; i < N; i++) {
count = count - a[i][1];
if (count == 0) {
rx = (int)round((a[i][0] + a[i][0]) / 2);
break;
}
if (count < 0) {
rx = a[i][0];
break;
}
}
}
if (sum % 2 == 1) {
count = (sum + 1) / 2;
for (i = 0; i < N; i++) {
count = count - b[i][1];
if (count == 0) {
ry = b[i][0];
break;
}
if (count < 0) {
ry = b[i - 1][0];
break;
}
}
}
if (sum % 2 == 0) {
count = sum / 2;
for (i = 0; i < N; i++) {
count = count - b[i][1];
if (count == 0) {
ry = (int)round((b[i][0] + b[i][0]) / 2);
break;
}
if (count < 0) {
ry = b[i][0];
break;
}
}
}
printf("%d %d", rx, ry);
return 0;
}
答案 0 :(得分:0)
我不理解您的方法,存在多个问题:
scanf()
的返回值,以避免在无效输入上发生未定义的行为。sum
的奇偶校验测试似乎无关。我将首先尝试暴力破解方法:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int N, R, S, i, row, col, best_row, best_col;
struct { int row, col, count; } *points;
long int sum, best_sum;
if (scanf("%d %d %d", &R, &S, &N) != 3 || R < 0 || S < 0 || N < 0) {
printf("invalid input\n");
return 1;
}
points = calloc(N, sizeof *points);
if (points == NULL) {
printf("allocation failure\n");
return 1;
}
for (i = 0; i < N; i++) {
if (scanf("%d %d %d", &points[i].row, &points[i].col, &points[i].count) != 3) {
printf("invalid input\n");
return 1;
}
}
best_row = best_col = -1;
best_sum = LONG_MAX;
for (row = 0; row < R; row++) {
for (col = 0; col < S; col++) {
sum = 0;
for (i = 0; i < N; i++) {
sum += points[i].count * (abs(row - points[i].row) + abs(col - points[i].col));
}
if (best_sum > sum) {
best_sum = sum;
best_row = row;
best_col = col;
}
}
}
printf("%d %d\n", best_row, best_col);
free(points);
return 0;
}
测试输入的输出:
1 1
时间复杂度令人恐惧,但对于小样本集而言并不重要。您可以通过分别计算水平距离和垂直距离之和来降低复杂度。