三星机试 最短路径

1.题目

Mr. Kim has to deliver refrigerators to N customers. From the office, he is going to visit all the customers and then return to his home. Each location of the office, his home, and the customers is given in the form of integer coordinates (x,y) (0≤x≤100, 0≤y≤100) . The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The locations of the office, his home, and the customers are all distinct. You should plan an optimal way to visit all the N customers and return to his among all the possibilities.

You are given the locations of the office, Mr. Kim’s home, and the customers; the number of the customers is in the range of 5 to 10. Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his home. Your program only have to report the distance of a (the) shortest path.

You don’t have to solve this problem efficiently. You could find an answer by looking up all the possible ways. If you can look up all the possibilities well, you will get a perfect score.

[Constraints]

5≤N≤10. Each location (x,y) is in a bounded grid, 0≤x≤100, 0≤y≤100, and x, y are integers.

[Input]

You are given 10 test cases. Each test case consists of two lines; the first line has N, the number of the customers, and the following line enumerates the locations of the office, Mr. Kim’s home, and the customers in sequence. Each location consists of the coordinates (x,y), which is reprensented by ‘x y’.

[Output]

Output the 10 answers in 10 lines. Each line outputs the distance of a (the) shortest path. Each line looks like ‘#x answer’ where x is the index of a test case. ‘#x’ and ‘answer’ are separated by a space.

[I/O Example]

Input (20 lines in total. In the first test case, the locations of the office and the home are (0, 0) and (100, 100) respectively, and the locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50, 20).)

5 ← Starting test case #1

0 0 100 100 70 40 30 10 10 5 90 70 50 20

6 ← Starting test case #2

88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14

10 ← Starting test case #3

39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36

...

#1 200

#2 304

#3 366

...
————————————————
原文链接:https://blog.youkuaiyun.com/Mr__________xiao/article/details/140384963

2.代码

不会 deepseek了

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x;
    int y;
} Point;

int distance(Point a, Point b) {
    return abs(a.x - b.x) + abs(a.y - b.y);
}

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int nextPermutation(int *arr, int N) {
    int i = N - 2;
    while (i >= 0 && arr[i] >= arr[i + 1]) {
        i--;
    }
    if (i < 0) return 0;

    int j = N - 1;
    while (arr[j] <= arr[i]) {
        j--;
    }
    swap(&arr[i], &arr[j]);

    int left = i + 1, right = N - 1;
    while (left < right) {
        swap(&arr[left], &arr[right]);
        left++;
        right--;
    }
    return 1;
}

void clear_input_buffer() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) {}  //清空缓冲区直到换行符或文件结束符
}

int calculateTotalDistance(Point *points, int *order, int N, int officeIndex, int homeIndex) {
    int totalDistance = distance(points[officeIndex], points[order[0]]);
    for (int i = 0; i < N - 1; i++) {
        totalDistance += distance(points[order[i]], points[order[i + 1]]);
    }
    totalDistance += distance(points[order[N - 1]], points[homeIndex]);
    return totalDistance;
}

int findShortestPath(Point *points, int N, int officeIndex, int homeIndex) {
    int *order = (int *)malloc(N * sizeof(int));
    for (int i = 0; i < N; i++) {
        order[i] = i + 2;
    }

    int shortestDistance = 1000000;
    do {
        int currentDistance = calculateTotalDistance(points, order, N, officeIndex, homeIndex);
        if (currentDistance < shortestDistance) {
            shortestDistance = currentDistance;
        }
    } while (nextPermutation(order, N));

    free(order);
    return shortestDistance;
}

int main() {
    Point points[12];
    int N;

    for (int testCase = 1; testCase <= 10; testCase++) {
        
        scanf(" %d\n", &N);
         
        scanf("%d %d", &points[0].x, &points[0].y); // 办公室
        scanf("%d %d", &points[1].x, &points[1].y); // 家

        for (int i = 2; i < N + 2; i++) {
            
            scanf("%d %d", &points[i].x, &points[i].y);
        }

        int shortestDistance = findShortestPath(points, N, 0, 1);

        printf("#%d %d\n", testCase, shortestDistance);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农珊珊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值