C 赋值循环和数组

问题描述 投票:0回答:0

(C语言。只有循环和数组讲座的知识) 需要帮助解决此类问题:

可以使用图形表示道路网络。假设我们有点/站 a, b, c, d, e、f、g 和 h,我们可以用箭头表示从一个点到另一个点的直接路径。

例如,根据下图:

• a点和b点、a点和f点、f点和b点之间存在双向路径 c点,d点和e点

• 从c点到i点存在单向路径,但i点到i点之间没有直接路径 c点

所有的节点都是点/目的地,但是黄色的特别代表充电 站。这些点/目的地之间的道路网络可以使用 布尔值(0 和 1)的邻接矩阵,如下所示。例如,a -> b = 1 和 b -> a = 1 假设 a 和 b 之间存在双向直接路径。同时,a -> c = 0 因为没有 a和c之间的直接路径。此外,a -> g = 0 但 g -> a = 1 因为存在单向路径 从g点到a点

VISUAL GUIDE

  a b c d e f g h
a 1 1 0 0 0 1 0 0
b 1 1 1 0 0 0 0 0
c 0 1 1 0 1 1 0 0
d 0 0 0 1 1 0 0 0
e 0 0 0 1 1 0 0 0
f 1 0 1 0 0 1 0 0
g 1 0 0 1 0 0 1 0
h 0 0 0 0 0 1 0 1

作为编程作业:

  1. 声明并初始化一个 road_networks 多维数组,表示 邻接矩阵
  2. 显示邻接矩阵。将括号放在考虑的点/目的地 作为充电站,例如[c], [d]
  3. 给定一个点/目的地,确定最近的充电站。例如,如果你是 在a点,最近的充电站是c点。如果你在e点,最近的充电 车站是d点。
  4. 奖励:使用宏定义二维数组的大小

测试用例:


测试案例1:

原点A

Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H

0
At point: A
point: C Arrived to charging station

测试案例2:

原点B

Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H

1
At point: B
point: C Arrived to charging station

测试案例3:

原点C

Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H

2
point: C is a charging station

测试案例4:

原点D

Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H

3
point: D is a charging station

测试案例5:

原点E

Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H

4
At point: E
point: D Arrived to charging station

测试案例6:

原点 F

Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H

5
At point: F
point: C Arrived to charging station

我无法理解问题的要点。

失败的尝试:

#include <stdio.h>
#define ROWS 8
#define COLS 8

int main() {
    int road_network[ROWS][COLS] = {
        {1, 1, 0, 0, 0, 1, 0, 0},
        {1, 1, 1, 0, 0, 0, 0, 0},
        {0, 1, 1, 0, 1, 1, 0, 0},
        {0, 0, 0, 1, 1, 0, 0, 0},
        {0, 0, 0, 1, 1, 0, 0, 0},
        {1, 0, 1, 0, 0, 1, 0, 0},
        {1, 0, 0, 1, 0, 0, 1, 0},
        {0, 0, 0, 0, 0, 1, 0, 1}
    };

    // Display the adjacency matrix
    printf("  a b c d e f g h\n");
    for (int i = 0; i < ROWS; i++) {
        printf("%c ", 'a' + i); // Print the point/destination label
        for (int j = 0; j < COLS; j++) {
            if (road_network[ROWS][COLS]) {
                printf("1 "); // Print 1 if there's a direct path
            } else {
                printf("0 "); // Print 0 if there's no direct path
            }
        }
       
       
        printf("\n");
    }

    int charging_stations[] = {2, 3};

    int current_location, nearest_charging_station;
    printf("Which point are you located? 0 - A, 1 - B, 2 - C, 3 - D, 4 - E, 5 - F, 6 - G, 7 - H\n");
    scanf("%d", &current_location);

    // Find the nearest charging station
    nearest_charging_station = -1;
    int min_distance = ROWS + COLS;
    for (int i = 0; i < sizeof(charging_stations)/sizeof(int); i++) {
        int station = charging_stations[i];
        if (road_network[current_location][station] == 1) {
            nearest_charging_station = station;
            break;
        }
        for (int j = 0; j < ROWS; j++) {
            if (road_network[current_location][j] == 1 && road_network[j][station] == 1) {
                int distance = j - current_location;
                if (distance < 0) {
                    distance = -distance;
                }
                if (distance < min_distance) {
                    min_distance = distance;
                    nearest_charging_station = station;
                }
            }
        }
    }

    if (nearest_charging_station != -1) {
        printf("At point: %c\npoint: %c Arrived to charging station\n", current_location + 'A', nearest_charging_station + 'A');
    } else {
        printf("At point: %c\npoint: %c is a charging station\n", current_location + 'A', current_location + 'A');
    }

    return 0;
}
c
© www.soinside.com 2019 - 2024. All rights reserved.