详细步骤:
将起始节点入队,拿出第一个节点,并将该节点周围的八个节点入队,判断这八个节点是不是在障碍或者已经在关列表中,如果满足则不在入队,如果是空白节点,则循环结束,输出该节点,否则按普通节点正常入队。
详细实现的步骤
结构体:
typedef struct Search_list
{
struct Search_list *next;
Point point;//表示坐标点x,y
}Search_list,* pSearch_list;
typedef struct Search_list search_list,* pSearch_list;
搜索的算法:返回1则搜索成功,0则没有可以清扫的区域
int map_search_blank_area()
{
start_point.x = (start_point.x / BLOCK_WIDTH) * BLOCK_WIDTH ;//取整
start_point.y = (start_point.y / BLOCK_WIDTH) * BLOCK_WIDTH ;
LOG_INFO("start_point[%d,%d]",start_point.x ,start_point.y);
pSearch_list open_list = (pSearch_list)malloc(sizeof(Search_list));//开列表
open_list->next = NULL;//千万不可以,NULL不可以访问的,除非open_list赋值
pSearch_list close_list = (pSearch_list)malloc(sizeof(Search_list));//关列表
close_list->next = NULL;
//创建起始节点并初始化,创建一个目标节点
pSearch_list start = (pSearch_list)malloc(sizeof(Search_list));
start->next = NULL;
start->point = start_point;//给起始点进行赋值
search_list_add_tail(open_list,start);//将起始点入队
int i,j;
int circle = 1;//循环
print_map();
while(circle)
{
if(open_list->next == NULL)//区域内没有未清扫的区域
{
LOG_INFO("search end point fail");
circle = 0;
search_blank_area = 0;
break;//跳出循环
}
//将当前点从open_list中移除,加入到close_list中,即为removeP
pSearch_list removeP = search_list_delete_point(open_list);//拿出队列中第一个点
LOG_INFO("search core is [%d,%d]",removeP->point.x ,removeP->point.y);
search_list_add_tail(close_list ,removeP);//将移除的这个点,加入到close_list
for(i = -1 ; i < 2 ; i++)//开始入队
{
for( j = -1 ; j < 2 ; j++)
{
Point temp ;
temp.x = removeP->point.x + i * BLOCK_WIDTH;
temp.y = removeP->point.y + j * BLOCK_WIDTH;
if(temp.x < (map_params.leftMax - COORD_FACTOR)*BLOCK_WIDTH || ( map_params.rightMax- COORD_FACTOR) *BLOCK_WIDTH < temp.x || temp.y < (map_params.downMax- COORD_FACTOR) *BLOCK_WIDTH || temp.y > (map_params.upMax - COORD_FACTOR) *BLOCK_WIDTH ){
LOG_INFO("is border!");//边界
continue;//向后继续循环
}
if(search_judge_node_exist(open_list,temp))//判断当前点不在open_list中
{
if(OBSTACLE == map_judge_area_can_go(temp)){//障碍物,不添加进open_list
LOG_INFO("point is obstacle [%d,%d]",temp.x ,temp.y);
continue;
}
else if(!search_judge_node_exist(close_list,temp))//在close_list中,已经从开列表中删除过了,不在重复入队
{
LOG_INFO("point is in close_list [%d,%d]",temp.x ,temp.y);
continue;
}
else if(map_judge_area_is_sweeped(temp))//当前区域是没有清扫的区域
{
LOG_INFO("find unsweep area! [%d,%d]",temp.x ,temp.y);
search_blank_area = 1 ;
end_point = temp;
circle = 0;
break;
}
else{//让这个点进行入队
LOG_INFO("point add in open_list [%d,%d]",temp.x ,temp.y);
pSearch_list pTmp = (pSearch_list)malloc(sizeof(Search_list));
pTmp->next = NULL;
pTmp->point.x = temp.x;
pTmp->point.y = temp.y;
search_list_add_tail(open_list,pTmp);
}
}
}
}
if(circle == 0 )
{
break;
}
}
if(search_blank_area)
{
return 1;
}
return 0;//搜索结束,区域已经完全结束
}