|
1 | | -#include <ctype.h> |
| 1 | +#include <math.h> |
2 | 2 | #include <stdbool.h> |
3 | 3 | #include <stdio.h> |
4 | 4 | #include <stdlib.h> |
5 | 5 | #include <string.h> |
6 | 6 |
|
7 | | -typedef struct |
8 | | -{ |
9 | | - int x; |
10 | | - int y; |
11 | | -} Point; |
| 7 | +#define MAX_NODES 100 |
12 | 8 |
|
13 | | -void printUsageAndExit() |
14 | | -{ |
15 | | - printf("Usage: please provide at least 3 x and y coordinates as separate " |
16 | | - "lists (e.g. \"100, 440, 210\")\n"); |
17 | | - exit(1); |
18 | | -} |
| 9 | +int adjacency_matrix[MAX_NODES][MAX_NODES]; |
| 10 | +int vertex_values[MAX_NODES]; |
| 11 | +int num_nodes; |
| 12 | +bool visited[MAX_NODES]; |
19 | 13 |
|
20 | | -int compare(const void *p1, const void *p2) |
| 14 | +bool dfs(int node, int target) |
21 | 15 | { |
22 | | - Point *point1 = (Point *)p1; |
23 | | - Point *point2 = (Point *)p2; |
24 | | - |
25 | | - if (point1->x != point2->x) |
26 | | - return point1->x - point2->x; |
27 | | - return point1->y - point2->y; |
28 | | -} |
29 | | - |
30 | | -int orientation(Point p, Point q, Point r) |
31 | | -{ |
32 | | - int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); |
33 | | - return (val == 0) ? 0 : (val > 0) ? 1 : 2; |
34 | | -} |
35 | | - |
36 | | -void convexHull(Point points[], int n) |
37 | | -{ |
38 | | - Point hull[n]; |
39 | | - int hullCount = 0; |
40 | | - |
41 | | - qsort(points, n, sizeof(Point), compare); |
42 | | - |
43 | | - int l = 0; |
44 | | - for (int i = 1; i < n; i++) |
45 | | - if (points[i].x < points[l].x) |
46 | | - l = i; |
47 | | - |
48 | | - int p = l, q; |
49 | | - do |
| 16 | + if (vertex_values[node] == target) |
| 17 | + return true; |
| 18 | + visited[node] = true; |
| 19 | + for (int i = 0; i < num_nodes; i++) |
50 | 20 | { |
51 | | - hull[hullCount++] = points[p]; |
52 | | - q = (p + 1) % n; |
53 | | - |
54 | | - for (int i = 0; i < n; i++) |
55 | | - if (orientation(points[p], points[i], points[q]) == 2) |
56 | | - q = i; |
57 | | - |
58 | | - p = q; |
59 | | - } while (p != l); |
60 | | - |
61 | | - for (int i = 0; i < hullCount; i++) |
62 | | - printf("(%d, %d)\n", hull[i].x, hull[i].y); |
63 | | -} |
64 | | - |
65 | | -// Function to check if a string is a valid number |
66 | | -bool isInteger(const char *s) |
67 | | -{ |
68 | | - char *end; |
69 | | - strtol(s, &end, 10); // Convert string to long |
70 | | - return (*end == '\0' |
71 | | - || *end == '\n'); // Check if the entire string was valid |
| 21 | + if (adjacency_matrix[node][i] && !visited[i]) |
| 22 | + { |
| 23 | + if (dfs(i, target)) |
| 24 | + return true; |
| 25 | + } |
| 26 | + } |
| 27 | + return false; |
72 | 28 | } |
73 | 29 |
|
74 | | -// Function to trim whitespace from a string |
75 | | -char *trimWhitespace(char *str) |
| 30 | +bool depth_first_search(int target) |
76 | 31 | { |
77 | | - // Trim leading whitespace |
78 | | - while (isspace((unsigned char)*str)) |
79 | | - str++; |
80 | | - // Trim trailing whitespace |
81 | | - char *end = str + strlen(str) - 1; |
82 | | - while (end > str && isspace((unsigned char)*end)) |
83 | | - end--; |
84 | | - *(end + 1) = '\0'; // Null terminate after the last non-space character |
85 | | - return str; |
| 32 | + memset(visited, 0, sizeof(visited)); |
| 33 | + for (int i = 0; i < num_nodes; i++) |
| 34 | + if (!visited[i] && dfs(i, target)) |
| 35 | + return true; |
| 36 | + return false; |
86 | 37 | } |
87 | 38 |
|
88 | | -// Function to parse input string and populate the array |
89 | | -int parseInput(const char *input, int **arr, int *size) |
| 39 | +void parse_matrix(char *input) |
90 | 40 | { |
91 | | - char *token; |
92 | | - int capacity = 10; // Initial capacity |
93 | | - *arr = malloc(capacity * sizeof(int)); |
94 | | - if (*arr == NULL) |
95 | | - return false; |
96 | | - |
97 | | - // Tokenize the input string based on commas |
98 | | - char *inputCopy = strdup(input); |
99 | | - if (inputCopy == NULL) |
100 | | - { |
101 | | - free(*arr); |
102 | | - *arr = NULL; |
103 | | - return false; |
104 | | - } |
105 | | - |
106 | | - token = strtok(inputCopy, ","); |
107 | | - *size = 0; |
108 | | - while (token) |
| 41 | + char *token = strtok(input, ", "); |
| 42 | + int i = 0, j = 0; |
| 43 | + while (token != NULL) |
109 | 44 | { |
110 | | - trimWhitespace(token); // Trim whitespace around token |
111 | | - if (!isInteger(token)) |
| 45 | + adjacency_matrix[i][j] = atoi(token); |
| 46 | + j++; |
| 47 | + if (j == num_nodes) |
112 | 48 | { |
113 | | - free(*arr); |
114 | | - free(inputCopy); |
115 | | - *arr = NULL; |
116 | | - return false; // Exit if a number is invalid |
| 49 | + i++; |
| 50 | + j = 0; |
117 | 51 | } |
118 | | - |
119 | | - if (*size >= capacity) |
120 | | - { |
121 | | - capacity *= 2; |
122 | | - *arr = realloc(*arr, capacity * sizeof(int)); |
123 | | - if (*arr == NULL) |
124 | | - { |
125 | | - free(inputCopy); |
126 | | - return false; |
127 | | - } |
128 | | - } |
129 | | - (*arr)[(*size)++] = atoi(token); |
130 | | - token = strtok(NULL, ","); |
| 52 | + token = strtok(NULL, ", "); |
131 | 53 | } |
132 | | - |
133 | | - // Resize the array to the actual size |
134 | | - *arr = realloc(*arr, *size * sizeof(int)); |
135 | | - free(inputCopy); // Free the input copy |
136 | | - if (*arr == NULL) |
137 | | - return false; |
138 | | - |
139 | | - return true; // Successful parsing |
140 | 54 | } |
141 | 55 |
|
142 | | -void parseCoordinates(char *inputX, char *inputY) |
| 56 | +void parse_values(char *input) |
143 | 57 | { |
144 | | - int *xCoords = NULL; |
145 | | - int *yCoords = NULL; |
146 | | - int xSize = 0; |
147 | | - int ySize = 0; |
148 | | - if (!parseInput(inputX, &xCoords, &xSize) |
149 | | - || !parseInput(inputY, &yCoords, &ySize) |
150 | | - || xSize != ySize |
151 | | - || xSize < 3) |
| 58 | + char *token = strtok(input, ", "); |
| 59 | + int i = 0; |
| 60 | + while (token != NULL) |
152 | 61 | { |
153 | | - if (xCoords != NULL) |
154 | | - free(xCoords); |
155 | | - |
156 | | - if (yCoords != NULL) |
157 | | - free(yCoords); |
158 | | - |
159 | | - printUsageAndExit(); |
160 | | - } |
161 | | - |
162 | | - int count = xSize; |
163 | | - Point *points = malloc(sizeof(Point) * count); |
164 | | - if (points == NULL) |
165 | | - { |
166 | | - free(xCoords); |
167 | | - free(yCoords); |
168 | | - printUsageAndExit(); |
| 62 | + vertex_values[i++] = atoi(token); |
| 63 | + token = strtok(NULL, ", "); |
169 | 64 | } |
| 65 | +} |
170 | 66 |
|
171 | | - for (int i = 0; i < count; i++) |
| 67 | +int main(int argc, char *argv[]) |
| 68 | +{ |
| 69 | + if (argc != 4 || !*argv[1] || !*argv[2] || !*argv[3]) |
172 | 70 | { |
173 | | - points[i].x = xCoords[i]; |
174 | | - points[i].y = yCoords[i]; |
| 71 | + printf("Usage: please provide a tree in an adjacency matrix form (\"0, " |
| 72 | + "1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, " |
| 73 | + "1, 0, 0\") together with a list of vertex values (\"1, 3, 5, " |
| 74 | + "2, 4\") and the integer to find (\"4\")"); |
| 75 | + return 1; |
175 | 76 | } |
176 | 77 |
|
177 | | - free(xCoords); |
178 | | - free(yCoords); |
| 78 | + char *matrix_str = argv[1]; |
| 79 | + char *values_str = argv[2]; |
| 80 | + int target = atoi(argv[3]); |
179 | 81 |
|
180 | | - convexHull(points, count); |
181 | | - free(points); |
182 | | -} |
| 82 | + int total_elements = 1; |
| 83 | + for (char *p = matrix_str; *p; p++) |
| 84 | + if (*p == ',') |
| 85 | + total_elements++; |
183 | 86 |
|
184 | | -int main(int argc, char *argv[]) |
185 | | -{ |
186 | | - if (argc != 3) |
187 | | - printUsageAndExit(); |
| 87 | + num_nodes = (int)sqrt(total_elements); |
188 | 88 |
|
189 | | - char *inputX = argv[1]; |
190 | | - char *inputY = argv[2]; |
| 89 | + parse_matrix(matrix_str); |
| 90 | + parse_values(values_str); |
191 | 91 |
|
192 | | - if (strlen(inputX) == 0 || strlen(inputY) == 0) |
193 | | - printUsageAndExit(); |
| 92 | + bool result = depth_first_search(target); |
| 93 | + printf(result ? "true\n" : "false\n"); |
194 | 94 |
|
195 | | - parseCoordinates(inputX, inputY); |
196 | 95 | return 0; |
197 | 96 | } |
0 commit comments