Reorganizing the code to make more sense.
This commit is contained in:
4 files changed
+65
-44
No files matched your search
+9
-3
@@ -39,10 +39,16 @@ void node_pool_destroy();
|
||||
int32_t node_pool_usage();
|
||||
|
||||
/**
|
||||
* @brief Tries to push child nodes based on the current node's move
|
||||
* @param curr The current node
|
||||
* @brief Returns the total capacity of the node pool
|
||||
* @return The total capacity of the node pool
|
||||
*/
|
||||
void try_push_children(Node* curr);
|
||||
int32_t node_pool_capacity();
|
||||
|
||||
/**
|
||||
* @brief Returns the next available node from the pool without incrementing the pointer
|
||||
* @return A pointer to the next available node, or NULL if the pool is exhausted
|
||||
*/
|
||||
Node* node_pool_next();
|
||||
|
||||
/**
|
||||
* @brief Marks a triplet as visited and checks if it's new
|
||||
|
||||
@@ -3,6 +3,12 @@
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
/**
|
||||
* @brief Tries to push child nodes based on the current node's move
|
||||
* @param curr The current node
|
||||
*/
|
||||
void try_push_children(Node* curr);
|
||||
|
||||
/**
|
||||
* @brief Runs a solver to find the minimum moves to reach a triplet with zero
|
||||
* @param sum Pointer to store the sum of moves for the found solution
|
||||
|
||||
+5
-41
@@ -58,49 +58,13 @@ int32_t node_pool_usage() {
|
||||
return pool_ptr;
|
||||
}
|
||||
|
||||
static void make_move(char move, Node* curr) {
|
||||
int128_t na = curr->a, nb = curr->b, nc = curr->c;
|
||||
|
||||
/* Calculate the new triplet based on the move type */
|
||||
if (move == 'A') {
|
||||
na = 2 * (curr->b + curr->c) - curr->a;
|
||||
} else if (move == 'B') {
|
||||
nb = 2 * (curr->a + curr->c) - curr->b;
|
||||
} else if (move == 'C') {
|
||||
nc = 2 * (curr->a + curr->b) - curr->c;
|
||||
}
|
||||
|
||||
/* Validation and Enqueue */
|
||||
if (safe_val(na) && safe_val(nb) && safe_val(nc)) {
|
||||
if (visit_and_mark(na, nb, nc)) {
|
||||
if (pool_ptr >= pool_capacity) return; /* Pool safety */
|
||||
Node* n = &pool[pool_ptr++];
|
||||
n->a = na; n->b = nb; n->c = nc;
|
||||
n->move = move;
|
||||
n->parent = curr;
|
||||
n->next = NULL;
|
||||
queue_push(n);
|
||||
}
|
||||
}
|
||||
int32_t node_pool_capacity() {
|
||||
return pool_capacity;
|
||||
}
|
||||
|
||||
void try_push_children(Node* curr) {
|
||||
char last = curr->move;
|
||||
|
||||
if (last == '\0') {
|
||||
make_move('A', curr);
|
||||
make_move('B', curr);
|
||||
make_move('C', curr);
|
||||
} else if (last == 'A') {
|
||||
make_move('C', curr);
|
||||
make_move('B', curr);
|
||||
} else if (last == 'B') {
|
||||
make_move('C', curr);
|
||||
make_move('A', curr);
|
||||
} else if (last == 'C') {
|
||||
make_move('B', curr);
|
||||
make_move('A', curr);
|
||||
}
|
||||
Node* node_pool_next() {
|
||||
if (pool_ptr >= pool_capacity) return NULL; /* Pool safety */
|
||||
return &pool[pool_ptr++];
|
||||
}
|
||||
|
||||
bool visit_and_mark(int128_t a, int128_t b, int128_t c) {
|
||||
|
||||
+45
@@ -7,6 +7,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void make_move(char move, Node* curr) {
|
||||
int128_t na = curr->a, nb = curr->b, nc = curr->c;
|
||||
|
||||
/* Calculate the new triplet based on the move type */
|
||||
if (move == 'A') {
|
||||
na = 2 * (curr->b + curr->c) - curr->a;
|
||||
} else if (move == 'B') {
|
||||
nb = 2 * (curr->a + curr->c) - curr->b;
|
||||
} else if (move == 'C') {
|
||||
nc = 2 * (curr->a + curr->b) - curr->c;
|
||||
}
|
||||
|
||||
/* Validation and Enqueue */
|
||||
if (safe_val(na) && safe_val(nb) && safe_val(nc)) {
|
||||
if (visit_and_mark(na, nb, nc)) {
|
||||
Node* n = node_pool_next();
|
||||
if (!n) return; /* Node pool exhausted, skip this move */
|
||||
n->a = na; n->b = nb; n->c = nc;
|
||||
n->move = move;
|
||||
n->parent = curr;
|
||||
n->next = NULL;
|
||||
queue_push(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void try_push_children(Node* curr) {
|
||||
char last = curr->move;
|
||||
|
||||
if (last == '\0') {
|
||||
make_move('A', curr);
|
||||
make_move('B', curr);
|
||||
make_move('C', curr);
|
||||
} else if (last == 'A') {
|
||||
make_move('C', curr);
|
||||
make_move('B', curr);
|
||||
} else if (last == 'B') {
|
||||
make_move('C', curr);
|
||||
make_move('A', curr);
|
||||
} else if (last == 'C') {
|
||||
make_move('B', curr);
|
||||
make_move('A', curr);
|
||||
}
|
||||
}
|
||||
|
||||
bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
||||
node_pool_reset();
|
||||
queue_clear();
|
||||
|
||||
Reference in new issue
Block a user