#include <stdio.h>
#include "maze.h"
#include "aov_element.h"
//迷宫算法:node 为一格地砖 status值为0表示没走过 1表示走过 type为地砖类型
// 向右走条件:next_node不为0 右边node->status值为0 type>=0 或 type = -3
// 向上走条件:up_node不为0 上方node->status值为 0 type >=0 或type = -3
// 向下走条件:down_node不为0 下方node->status 值为 0 type >=0或type = -3
// 向左走条件:previous_node不为0 左边node->status值为 0 type >=0或type = -3
class Aov_Element;
int AOV::find(Node *node,QStringList *list)
{
node->status = CHECKED;
int find_result=0;
int result;
result = find_right(node,list);
if(result == 1)//已到达边界线并回到前几个node
find_result = 1;
else
if(result == 2)
{
find_result = 1;
return find_result;
}
if(find_buttom(node,list))find_result = 1;
if(find_left(node,list))find_result = 1;
if(find_top(node,list))find_result = 1;
node->status = UNCHECK;
return find_result;
}
int AOV::find_right(Node *node, QStringList *list)
{
Node *node1;
if(!node->next_node)
{
node->status = UNCHECK;//到达尽头,status重置
return 2;
}
if(node->next_node->type == -2 ||node->next_node->type == -1 || node->next_node->status == CHECKED)
return 0;
else
if(node->next_node){
// printf("right\n");
if(node->next_node->type == 0 || node->next_node->type == -3)
{
if(node->type == -3 || node->type == 0)//判断前一个node类型
{
node1 = node->next_node;
if(!node->front_node_list.isEmpty())
{
node1->front_node_list.append(node->front_node_list.last());
node1->front_position_list.append(node->front_position_list.last());
}
if(!find(node1,list))
{
if(!node1->front_node_list.isEmpty())
{
node1->front_node_list.removeLast();
node1->front_position_list.removeLast();
}
return 0;
}
}
else
if(node->type > 0)
{
node1 = node->next_node;
node1->front_node_list.append(node);
node1->front_position_list.append(RIGHT);
if(!find(node1,list))
{
if(!node1->front_node_list.isEmpty())
{
node1->front_node_list.removeLast();
node1->front_position_list.removeLast();
}
return 0;
}
}
}
else
if(node->next_node->type >0)
{
int flag = 0;
if(!node->front_node_list.isEmpty())
{
if(!node->next_node->front_node_list.contains(node->front_node_list.last()))
{
node->next_node->front_node_list.append(node->front_node_list.last());
node->next_node->front_position_list.append(node->front_position_list.last());
flag = 1;
}
if(!node->front_node_list.last()->next_node_list.contains(node->next_node))
{
node->front_node_list.last()->next_node_list.append(node->next_node);
node->front_node_list.last()->next_position_list.append(LEFT);
}
}
node1 = node->next_node;
list->append(node1->command);
if(!find(node1,list))
{
if(!node1->front_node_list.isEmpty())
{
if(flag)
{
node1->front_node_list.last()->next_node_list.removeLast();
node1->front_node_list.last()->next_position_list.removeLast();
node1->front_node_list.removeLast();
node1->front_position_list.removeLast();
}
}
return 0;
}
}
return 1;
}
}
int AOV::find_buttom(Node *node, QStringList *list)
{
Node *node1;
int num = 0,i;
if(!node->down_node)
return 0;
if(node->down_node->status == CHECKED)
return 0;
else
{
// printf("buttom\n");
if(!node->front_node_list.isEmpty())
{
node->down_node->front_node_list.append(node->front_node_list.last());
node->down_node->front_position_list.append(node->front_position_list.last());
}
node1 = node->down_node;
if(!find(node1,list))
{
if(!node1->front_node_list.isEmpty())
{
node1->front_node_list.removeLast();
node1->front_position_list.removeLast();
}
return 0;
}
else
{
return 1;
}
}
}
int AOV::find_left(Node *node, QStringList *list)
{
Node *node1;
if(!node->previous_node)
{
return 0;
}
if(node->previous_node->type == -2 ||node->previous_node->type == -1 || node->previous_node->status == CHECKED)
return 0;
else
if(node->previous_node){
// printf("left\n");
if(node->previous_node->type == 0 || node->previous_node->type == -3)
{
if(node->type == -3 || node->type == 0)//判断前一个node类型
{
node1 = node->previous_node;
if(!node->front_node_list.isEmpty())
{
node1->front_node_list.append(node->front_node_list.last());
node1->front_position_list.append(node->front_position_list.last());
}
if(!find(node1,list))
{
if(!node1->front_node_list.isEmpty())
{
node1->front_node_list.removeLast();
node1->front_position_list.removeLast();
}
return 0;
}
}
else
if(node->type > 0)
{
node1 = node->previous_node;
node1->front_node_list.append(node);
node1->front_position_list.append(LEFT);
if(!find(node1,list))
{
if(!node1->front_node_list.isEmpty())
{
node1->front_node_list.removeLast();
node1->front_position_list.removeLast();
}
return 0;
}
}
}
else
if(node->previous_node->type >0)
{
int flag = 0;
if(!node->front_node_list.isEmpty())
{
if(!node->previous_node->front_node_list.contains(node->front_node_list.last()))
{
node->previous_node->front_node_list.append(node->front_node_list.last());
node->previous_node->front_position_list.append(node->front_position_list.last());
flag = 1;
}
if(!node->front_node_list.last()->next_node_list.contains(node->previous_node))
{
node->front_node_list.last()->next_node_list.append(node->previous_node);
node->front_node_list.last()->next_position_list.ap
- 1
- 2
前往页