Skip to content

Commit 8365859

Browse files
committed
Add implementation for "Queen's Attack II" problem
This commit introduces a C program to solve the "Queen's Attack II" problem. It calculates the queen's possible moves on a chessboard while accounting for obstacles. The solution includes utility functions for input handling and processing the constraints.
1 parent 51d76a7 commit 8365859

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

QueensAttack2.c

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#include <assert.h>
2+
#include <ctype.h>
3+
#include <limits.h>
4+
#include <math.h>
5+
#include <stdbool.h>
6+
#include <stddef.h>
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
char* readline();
13+
char* ltrim(char*);
14+
char* rtrim(char*);
15+
char** split_string(char*);
16+
17+
int parse_int(char*);
18+
19+
/*
20+
* Complete the 'queensAttack' function below.
21+
*
22+
* The function is expected to return an INTEGER.
23+
* The function accepts following parameters:
24+
* 1. INTEGER n
25+
* 2. INTEGER k
26+
* 3. INTEGER r_q
27+
* 4. INTEGER c_q
28+
* 5. 2D_INTEGER_ARRAY obstacles
29+
*/
30+
31+
int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int** obstacles) {
32+
// Initialize movement limits in 8 directions
33+
int up = n - r_q;
34+
int down = r_q - 1;
35+
int left = c_q - 1;
36+
int right = n - c_q;
37+
38+
int top_left = (r_q - 1 < c_q - 1) ? r_q - 1 : c_q - 1;
39+
int top_right = (r_q - 1 < n - c_q) ? r_q - 1 : n - c_q;
40+
int bottom_left = (n - r_q < c_q - 1) ? n - r_q : c_q - 1;
41+
int bottom_right = (n - r_q < n - c_q) ? n - r_q : n - c_q;
42+
43+
// Process each obstacle
44+
for (int i = 0; i < k; i++) {
45+
int obs_row = obstacles[i][0];
46+
int obs_col = obstacles[i][1];
47+
48+
// Check if obstacle affects the column (up or down)
49+
if (obs_col == c_q) {
50+
if (obs_row > r_q) {
51+
up = (obs_row - r_q - 1 < up) ? obs_row - r_q - 1 : up;
52+
} else {
53+
down = (r_q - obs_row - 1 < down) ? r_q - obs_row - 1 : down;
54+
}
55+
}
56+
// Check if obstacle affects the row (left or right)
57+
else if (obs_row == r_q) {
58+
if (obs_col > c_q) {
59+
right = (obs_col - c_q - 1 < right) ? obs_col - c_q - 1 : right;
60+
} else {
61+
left = (c_q - obs_col - 1 < left) ? c_q - obs_col - 1 : left;
62+
}
63+
}
64+
// Check if obstacle affects diagonals
65+
else if (abs(obs_row - r_q) == abs(obs_col - c_q)) {
66+
// Top-left diagonal
67+
if (obs_row < r_q && obs_col < c_q) {
68+
top_left = (r_q - obs_row - 1 < top_left) ? r_q - obs_row - 1 : top_left;
69+
}
70+
// Top-right diagonal
71+
else if (obs_row < r_q && obs_col > c_q) {
72+
top_right = (r_q - obs_row - 1 < top_right) ? r_q - obs_row - 1 : top_right;
73+
}
74+
// Bottom-left diagonal
75+
else if (obs_row > r_q && obs_col < c_q) {
76+
bottom_left = (obs_row - r_q - 1 < bottom_left) ? obs_row - r_q - 1 : bottom_left;
77+
}
78+
// Bottom-right diagonal
79+
else if (obs_row > r_q && obs_col > c_q) {
80+
bottom_right = (obs_row - r_q - 1 < bottom_right) ? obs_row - r_q - 1 : bottom_right;
81+
}
82+
}
83+
}
84+
85+
// Sum up all valid moves
86+
return up + down + left + right + top_left + top_right + bottom_left + bottom_right;
87+
}
88+
89+
int main()
90+
{
91+
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
92+
93+
char** first_multiple_input = split_string(rtrim(readline()));
94+
95+
int n = parse_int(*(first_multiple_input + 0));
96+
97+
int k = parse_int(*(first_multiple_input + 1));
98+
99+
char** second_multiple_input = split_string(rtrim(readline()));
100+
101+
int r_q = parse_int(*(second_multiple_input + 0));
102+
103+
int c_q = parse_int(*(second_multiple_input + 1));
104+
105+
int** obstacles = malloc(k * sizeof(int*));
106+
107+
for (int i = 0; i < k; i++) {
108+
*(obstacles + i) = malloc(2 * (sizeof(int)));
109+
110+
char** obstacles_item_temp = split_string(rtrim(readline()));
111+
112+
for (int j = 0; j < 2; j++) {
113+
int obstacles_item = parse_int(*(obstacles_item_temp + j));
114+
115+
*(*(obstacles + i) + j) = obstacles_item;
116+
}
117+
}
118+
119+
int result = queensAttack(n, k, r_q, c_q, k, 2, obstacles);
120+
121+
fprintf(fptr, "%d\n", result);
122+
123+
fclose(fptr);
124+
125+
return 0;
126+
}
127+
128+
char* readline() {
129+
size_t alloc_length = 1024;
130+
size_t data_length = 0;
131+
132+
char* data = malloc(alloc_length);
133+
134+
while (true) {
135+
char* cursor = data + data_length;
136+
char* line = fgets(cursor, alloc_length - data_length, stdin);
137+
138+
if (!line) {
139+
break;
140+
}
141+
142+
data_length += strlen(cursor);
143+
144+
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
145+
break;
146+
}
147+
148+
alloc_length <<= 1;
149+
150+
data = realloc(data, alloc_length);
151+
152+
if (!data) {
153+
data = '\0';
154+
155+
break;
156+
}
157+
}
158+
159+
if (data[data_length - 1] == '\n') {
160+
data[data_length - 1] = '\0';
161+
162+
data = realloc(data, data_length);
163+
164+
if (!data) {
165+
data = '\0';
166+
}
167+
} else {
168+
data = realloc(data, data_length + 1);
169+
170+
if (!data) {
171+
data = '\0';
172+
} else {
173+
data[data_length] = '\0';
174+
}
175+
}
176+
177+
return data;
178+
}
179+
180+
char* ltrim(char* str) {
181+
if (!str) {
182+
return '\0';
183+
}
184+
185+
if (!*str) {
186+
return str;
187+
}
188+
189+
while (*str != '\0' && isspace(*str)) {
190+
str++;
191+
}
192+
193+
return str;
194+
}
195+
196+
char* rtrim(char* str) {
197+
if (!str) {
198+
return '\0';
199+
}
200+
201+
if (!*str) {
202+
return str;
203+
}
204+
205+
char* end = str + strlen(str) - 1;
206+
207+
while (end >= str && isspace(*end)) {
208+
end--;
209+
}
210+
211+
*(end + 1) = '\0';
212+
213+
return str;
214+
}
215+
216+
char** split_string(char* str) {
217+
char** splits = NULL;
218+
char* token = strtok(str, " ");
219+
220+
int spaces = 0;
221+
222+
while (token) {
223+
splits = realloc(splits, sizeof(char*) * ++spaces);
224+
225+
if (!splits) {
226+
return splits;
227+
}
228+
229+
splits[spaces - 1] = token;
230+
231+
token = strtok(NULL, " ");
232+
}
233+
234+
return splits;
235+
}
236+
237+
int parse_int(char* str) {
238+
char* endptr;
239+
int value = strtol(str, &endptr, 10);
240+
241+
if (endptr == str || *endptr != '\0') {
242+
exit(EXIT_FAILURE);
243+
}
244+
245+
return value;
246+
}

0 commit comments

Comments
 (0)