From 95c466df9574a6d9ca5906dfda8da54b6a4d548b Mon Sep 17 00:00:00 2001 From: 189****3067 <3347860465@qq.com> Date: Fri, 22 Dec 2023 08:01:55 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E4=B8=AD=E7=9A=84=E6=9C=80=E7=9F=AD=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\347\237\255\350\267\257\345\276\204.cpp" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" diff --git "a/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" new file mode 100644 index 00000000..a342a6ff --- /dev/null +++ "b/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" @@ -0,0 +1,71 @@ +#include +#include +#include +using namespace std; + +struct Point { + int x; + int y; + int dist; + Point(int _x, int _y, int _dist) : x(_x), y(_y), dist(_dist) {} +}; + +int shortestPathBinaryMatrix(vector>& grid) { + int m = grid.size(); + int n = grid[0].size(); + + if (grid[0][0] == 1 || grid[m-1][n-1] == 1) { + return -1; + } + + vector> visited(m, vector(n, 0)); + + queue q; + q.push(Point(0, 0, 1)); + + while (!q.empty()) { + Point p = q.front(); + q.pop(); + + int x = p.x; + int y = p.y; + int dist = p.dist; + + if (x == m-1 && y == n-1) { + return dist; + } + + if (visited[x][y]) { + continue; + } + visited[x][y] = 1; + + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + int nx = x + dx; + int ny = y + dy; + + if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny] && grid[nx][ny] != 1) { + q.push(Point(nx, ny, dist+1)); + } + } + } + } + + return -1; +} + +int main() { + vector> grid = { + {0, 1, 0, 0}, + {0, 0, 0, 1}, + {0, 1, 0, 0}, + {0, 0, 1, 0} + }; + + int shortestPath = shortestPathBinaryMatrix(grid); + + cout << "最短路径长度为:" << shortestPath << endl; + + return 0; +} \ No newline at end of file -- Gitee From 4d1470c2abe05aa6a567b1008e3384830b55d709 Mon Sep 17 00:00:00 2001 From: 189****3067 <3347860465@qq.com> Date: Fri, 22 Dec 2023 08:05:02 +0000 Subject: [PATCH 2/6] lc1091 --- ...\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" | 1 + 1 file changed, 1 insertion(+) diff --git "a/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" index b2e965d2..c53467fb 100644 --- "a/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" +++ "b/2209040070/chapter_8/lc_1091\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" @@ -22,6 +22,7 @@ int shortestPathBinaryMatrix(vector>& grid) { queue q; q.push(Point(0, 0, 1)); + while (!q.empty()) { Point p = q.front(); q.pop(); -- Gitee From 9f436e645280a395fab07f29000c873e422e4e42 Mon Sep 17 00:00:00 2001 From: 189****3067 <3347860465@qq.com> Date: Fri, 22 Dec 2023 08:07:55 +0000 Subject: [PATCH 3/6] lc_997 --- ...7\347\232\204\346\263\225\345\256\230.cpp" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "2209040070/chapter_8/lc_997\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" diff --git "a/2209040070/chapter_8/lc_997\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" "b/2209040070/chapter_8/lc_997\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" new file mode 100644 index 00000000..92718233 --- /dev/null +++ "b/2209040070/chapter_8/lc_997\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" @@ -0,0 +1,21 @@ +int findJudge(int n, int** trust, int trustSize, int* trustColSize){ + int *a=malloc(sizeof(int)*(n+1)); + int *b=malloc(sizeof(int)*(n+1)); + int m=0; + for(int i=1;i<=n;i++) + a[i]=0; + for(int i=1;i<=n;i++) + b[i]=0; + for(int i=0;i=n-1) + return i; + return -1; +} \ No newline at end of file -- Gitee From d95077aa3237f059ee13c66374cc4db7be71df6e Mon Sep 17 00:00:00 2001 From: 189****3067 <3347860465@qq.com> Date: Fri, 22 Dec 2023 08:08:30 +0000 Subject: [PATCH 4/6] lc_785 --- ...5\344\272\214\345\210\206\345\233\276.cpp" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "2209040070/chapter_8/lc_785\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" diff --git "a/2209040070/chapter_8/lc_785\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" "b/2209040070/chapter_8/lc_785\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" new file mode 100644 index 00000000..f9409d15 --- /dev/null +++ "b/2209040070/chapter_8/lc_785\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" @@ -0,0 +1,30 @@ +bool dfs(int node, int c, int* color, int** graph, int* graphColSize) { + color[node] = c; + int N = (c == 1 ? 2 : 1); + for (int i = 0; i < graphColSize[node]; ++i) { + int n = graph[node][i]; + if (color[n] == 0) { + if (!dfs(n, N, color, graph, graphColSize)) { + return false; + } + } else if (color[n] != N) { + return false; + } + } + return true; +} + +bool isBipartite(int** graph, int graphSize, int* graphColSize) { + int* color = (int*)malloc(sizeof(int) * graphSize); + memset(color, 0, sizeof(int) * graphSize); + for (int i = 0; i < graphSize; ++i) { + if (color[i] == 0) { + if (!dfs(i, 1, color, graph, graphColSize)) { + free(color); + return false; + } + } + } + free(color); + return true; +} \ No newline at end of file -- Gitee From 3cd85f50411c7efa112d58ae9142608a00820ac6 Mon Sep 17 00:00:00 2001 From: 189****3067 <3347860465@qq.com> Date: Fri, 22 Dec 2023 08:10:08 +0000 Subject: [PATCH 5/6] lc_207 --- ...7\350\257\276\347\250\213\350\241\250.cpp" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "2209040070/chapter_8/lc_207\350\257\276\347\250\213\350\241\250.cpp" diff --git "a/2209040070/chapter_8/lc_207\350\257\276\347\250\213\350\241\250.cpp" "b/2209040070/chapter_8/lc_207\350\257\276\347\250\213\350\241\250.cpp" new file mode 100644 index 00000000..009479fe --- /dev/null +++ "b/2209040070/chapter_8/lc_207\350\257\276\347\250\213\350\241\250.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include +using namespace std; + +bool canFinish(int numCourses, vector>& prerequisites) { + vector indegree(numCourses, 0); + vector> graph(numCourses); + + for (auto edge : prerequisites) { + int course = edge[0]; + int prerequisite = edge[1]; + + graph[prerequisite].push_back(course); + indegree[course]++; + } + + queue q; + + for (int i = 0; i < numCourses; i++) { + if (indegree[i] == 0) { + q.push(i); + } + } + + int finishedCourses = 0; + + while (!q.empty()) { + int course = q.front(); + q.pop(); + finishedCourses++; + + for (int neighbor : graph[course]) { + indegree[neighbor]--; + if (indegree[neighbor] == 0) { + q.push(neighbor); + } + } + } + + return finishedCourses == numCourses; +} + +int main() { + int numCourses = 4; + vector> prerequisites = {{1, 0}, {2, 1}, {3, 2}, {0, 3}}; + + if (canFinish(numCourses, prerequisites)) { + cout << "可以完成课程" << endl; + } else { + cout << "无法完成课程" << endl; + } + + return 0; +} \ No newline at end of file -- Gitee From 139cf7ec6722657ced02bfba983beadbb0407b00 Mon Sep 17 00:00:00 2001 From: 189****3067 <3347860465@qq.com> Date: Fri, 22 Dec 2023 08:11:01 +0000 Subject: [PATCH 6/6] lc_1584 --- ...0\345\260\217\350\264\271\347\224\250.cpp" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "2209040070/chapter_8/lc_1584\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" diff --git "a/2209040070/chapter_8/lc_1584\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" "b/2209040070/chapter_8/lc_1584\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" new file mode 100644 index 00000000..80a84315 --- /dev/null +++ "b/2209040070/chapter_8/lc_1584\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" @@ -0,0 +1,73 @@ +#include +#include +#include + +using namespace std; + +const int INF = 1e9; + +struct Edge { + int to, cost; +}; + +int prim(vector>& graph, int n) { + vector dist(n, INF); + vector visited(n, false); + int minCost = 0; + + priority_queue, vector>, greater>> pq; + + dist[0] = 0; + pq.push(make_pair(0, 0)); + + while (!pq.empty()) { + int u = pq.top().second; + pq.pop(); + + if (visited[u]) { + continue; + } + + visited[u] = true; + minCost += dist[u]; + + for (auto& edge : graph[u]) { + int v = edge.to; + int cost = edge.cost; + + if (!visited[v] && cost < dist[v]) { + dist[v] = cost; + pq.push(make_pair(dist[v], v)); + } + } + } + + return minCost; +} + +int main() { + int n; + cout << "请输入点的数量:"; + cin >> n; + + vector> graph(n, vector()); + + cout << "请依次输入各个点之间的距离:" << endl; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + int cost; + cin >> cost; + + if (i != j) { + graph[i].push_back({j, cost}); + } + } + } + + int minCost = prim(graph, n); + + cout << "连接所有点的最小费用为:" << minCost << endl; + + return 0; +} \ No newline at end of file -- Gitee