LeetCode //C - 547. Number of Provinces

该篇文章介绍了如何利用深度优先搜索(DFS)算法解决LeetCode中的题目547,即给定一个城市之间的连接矩阵,计算连通省份的数量。通过遍历矩阵并标记已访问的城市,找出每个完整的省份并计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

547. Number of Provinces

There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.

A province is a group of directly or indirectly connected cities and no other cities outside of the group.

You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.

Return the total number of provinces.
 

Example 1:

Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2

Example 2:

Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3

Constraints:
  • 1 <= n <= 200
  • n == isConnected.length
  • n == isConnected[i].length
  • isConnected[i][j] is 1 or 0.
  • isConnected[i][i] == 1
  • isConnected[i][j] == isConnected[j][i]

From: LeetCode
Link: 547. Number of Provinces


Solution:

Ideas:

1. Function Definition: findCircleNum is the function that takes in the adjacency matrix (isConnected), its size (isConnectedSize), and a pointer to an array representing the size of each row (isConnectedColSize).

2. Visited Array: We create an array called visited to keep track of which cities have been visited during the DFS. This array has one entry per city, initialized to 0 (indicating no cities have been visited at the start).

3. DFS Function: The dfs function is a recursive function that marks the current city as visited and then looks for all cities directly connected to it that have not yet been visited. It calls itself recursively for each unvisited city, effectively traversing the entire province.

4. Counting Provinces: In the findCircleNum function, we iterate over each city. If a city hasn’t been visited yet, we call the dfs function on it, which will visit all cities in its province. Once we return from the dfs call, we know we’ve fully visited one province, so we increment our province count.

5. Main Function: The main function is just an example of how to use findCircleNum. It creates an adjacency matrix for a set of cities, calls the findCircleNum function to find the number of provinces, and then frees the allocated memory.

Code:
void dfs(int** isConnected, int isConnectedSize, int* visited, int i) {
    for (int j = 0; j < isConnectedSize; j++) {
        if (isConnected[i][j] == 1 && !visited[j]) {
            visited[j] = 1;
            dfs(isConnected, isConnectedSize, visited, j);
        }
    }
}

int findCircleNum(int** isConnected, int isConnectedSize, int* isConnectedColSize) {
    int provinces = 0;
    int *visited = (int *)calloc(isConnectedSize, sizeof(int));

    for (int i = 0; i < isConnectedSize; i++) {
        if (!visited[i]) {
            dfs(isConnected, isConnectedSize, visited, i);
            provinces++;
        }
    }

    free(visited);
    return provinces;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Navigator_Z

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

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

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

打赏作者

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

抵扣说明:

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

余额充值