在栅格地图中搜索出空白区域

详细步骤:
将起始节点入队,拿出第一个节点,并将该节点周围的八个节点入队,判断这八个节点是不是在障碍或者已经在关列表中,如果满足则不在入队,如果是空白节点,则循环结束,输出该节点,否则按普通节点正常入队。
详细实现的步骤
结构体:

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;//搜索结束,区域已经完全结束
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾许人间第一流.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值