c - 节点结构在扩展期间获得自己作为父节点
问题描述
我的任务是为 3x3 滑动瓷砖拼图编写一个求解器,该拼图必须稍后嵌入。
我有结构
struct node
{
char state[9];
struct node* parentNode;
char lastMove;
} node;
我使用伪堆栈
struct node
{
char state[9];
struct node* parentNode;
char lastMove;
} node;
我使用了一个堆栈,稍后将用于 IDS 算法。
struct node stack[60000];
struct node* Node = stack;
#define push(Node, n) (*((Node)++) = (n))
#define pop(Node) (*--(Node))
我的函数 expandNode 创建其给定父节点的所有有效子节点
void expandNode(struct node* parent)
{
char help;
int emptyField = -1;
struct node newNode;
for (int i = 0; i < 9; i++)
{
if (parent->state[i] == '0') emptyField = i;
}
for (int i = 0; i < 4; i++)
{
if(lastMoveVal(parent->lastMove, moves[i]) == '0') continue;
if (moveValid(parent->state, moves[i]) == '1')
{
newNode.parentNode = parent;
...
我的主要功能
void main()
{
struct node root;
char rootState[9] = { '6','8','3','0','4','5','1','7','2' };
assignArray(root.state, rootState);
push(Node, root);
struct node curr;
curr = pop(Node);
expandNode(&curr);
for(int i = 0; i < 10; i++)
{
curr = pop(Node);
printf("\n");
for(int j = 0; j<9; j++)
{
printf("%c", curr.state[j]);
};
printf(" PARENT: ");
for(int j = 0; j<9; j++)
{
printf("%c", curr.parentNode->state[j]);
};
}
}
这是输出:
683405172 父:683405172
683145072 父:683145072
083645172 父:083645172
如您所见,节点的扩展是正确的,但父节点的状态不是预期的 683045172 而是节点本身的状态。
解决方案
推荐阅读
- amazon-s3 - 当 https 添加到 URL 时,CloudFront 会给出 ERR_CONNECTION_REFUSED
- visual-studio - 如何设置“vcvarsall.bat” - 将输出(目标)架构构建为“任何 CPU”
- node.js - 如何正确配置 swagger-jsdoc 以使用基本身份验证?
- python - 将TXT文件转换为浮点数组python
- java - Gson 流 api 没有读取整个文档
- python - Groupby 与 True, False & Both?
- browser - HttpClient 返回 403('Forbidden')但通过浏览器工作(都使用代理服务器)
- http-headers - 如何在不编写 Java 代码的情况下配置 Glassfish 4 以包含简单静态网站的某些标头?
- json - 任何人都可以访问主机中受保护的(使用 .htaccess)json 文件吗?
- java - E/RecyclerView:没有附加适配器;使用 API 29 跳过布局错误 Android Studio