Cleaning Robot (bfs+dfs)

本文介绍了一种使用广度优先搜索(BFS)和深度优先搜索(DFS)算法解决清洁机器人在带有家具的矩形房间内清洁脏瓷砖的问题。通过计算机器人从初始位置到所有脏瓷砖的最短路径,实现所有脏瓷砖的清洁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cleaning Robot (bfs+dfs)
Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture. 

Consider the room floor paved with square tiles whose size fits the cleaning robot (1 * 1). There are 'clean tiles' and 'dirty tiles', and the robot can change a 'dirty tile' to a 'clean tile' by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more. 

Your task is to write a program which computes the minimum number of moves for the robot to change all 'dirty tiles' to 'clean tiles', if ever possible.

Input

The input consists of multiple maps, each representing the size and arrangement of the room. A map is given in the following format. 

w h 
c11 c12 c13 ... c1w 
c21 c22 c23 ... c2w 
... 
ch1 ch2 ch3 ... chw 

The integers w and h are the lengths of the two sides of the floor of the room in terms of widths of floor tiles. w and h are less than or equal to 20. The character cyx represents what is initially on the tile with coordinates (x, y) as follows. 

'.' : a clean tile 
'*' : a dirty tile 
'x' : a piece of furniture (obstacle) 
'o' : the robot (initial position) 

In the map the number of 'dirty tiles' does not exceed 10. There is only one 'robot'. 

The end of the input is indicated by a line containing two zeros.

Output

For each map, your program should output a line containing the minimum number of moves. If the map includes 'dirty tiles' which the robot cannot reach, your program should output -1.

Sample Input

7 5
.......
.o...*.
.......
.*...*.
.......
15 13
.......x.......
...o...x....*..
.......x.......
.......x.......
.......x.......
...............
xxxxx.....xxxxx
...............
.......x.......
.......x.......
.......x.......
..*....x....*..
.......x.......
10 10
..........
..o.......
..........
..........
..........
.....xxxxx
.....x....
.....x.*..
.....x....
.....x....
0 0

Sample Output

8
49
-1
///bfs搜出点与点之间的距离,查找是否联系,然后用dfs来查找最短的点 
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
char mp[50][50];
int dis[50][50];
int vis[50][50];
int tag[50][50];
const int inf = 999999999;
struct node
{
    int x,y,step;
 } point[500];
 
int w,h,cnt,ans;
void bfs(node fir,int pt)//通过bfs来记录下所有的点的位置 
{
    queue <node>s;
    fir.step=0;
    while(!s.empty())
    s.pop();
    vis[fir.x][fir.y]=1;
    s.push(fir);
    while(!s.empty())
    {
        node t = s.front();
        s.pop();
        if(mp[t.x][t.y]=='*'||mp[t.x][t.y]=='o')
        dis[pt][tag[t.x][t.y]]=t.step;
        int next[4][2]={0,1,0,-1,1,0,-1,0};
        for(int i=0;i<4;i++)
        {
            node temp = t;
            temp.x+=next[i][0];
            temp.y+=next[i][1];
               if(temp.x<1||temp.y<1||temp.x>h||temp.y>w||vis[temp.x][temp.y]==1||mp[temp.x][temp.y]=='x')
            {
                continue;
            }
            temp.step+=1;
            s.push(temp);
            vis[temp.x][temp.y]=1;
        }
    }
}
int vist[50];
void dfs(int x,int step,int s)
{
    if(step==cnt)
    {
        ans=min(s,ans);
        return ;
    }
    if(s>ans)
    return ;
    for(int i=1;i<=cnt;i++)
    {
        if(vist[i])
        continue ;
        vist[i]=1;
        dfs(i,step+1,s+dis[x][i]);
        vist[i]=0;
    }
}
int main()
{
    while(scanf("%d%d",&w,&h))
    {
        if(w==0&&h==0)
        break;
        cnt = 0;
        getchar();
        memset(point,0,sizeof(point));
        memset(tag,0,sizeof(tag));
        memset(dis,0,sizeof(dis));
        for(int i=1;i<=h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='*')
                {
                    point[++cnt].x=i;
                    point[cnt].y=j;
                    tag[i][j]=cnt;
                }
                else if(mp[i][j]=='o')
                {
                    tag[i][j]=0;
                    point[0].x=i;
                    point[0].y=j;
                }
            }
            getchar();
        }
         for(int i=0;i<=cnt;i++)
        {
            for(int j=0;j<=cnt;j++)
            {
                if(i!=j)
                    dis[i][j]=inf;
                else
                    dis[i][j]=0;
            }
        }
        for(int i=0;i<=cnt;i++)
        {
            memset(vis,0,sizeof(vis));
            bfs( point[i],i );
        }

        bool flag=1;
        for(int i=0;i<=cnt && flag;i++)
            for(int j=0;j<=cnt && flag;j++)
                if(dis[i][j]==inf)
                    flag=0;
        if(!flag)
        {
            printf("-1\n");
            continue;
        }
        memset(vist,0,sizeof(vist));
        vist[0]=1;
        ans=inf;
        dfs(0,0,0);
        printf("%d\n",ans);


    }
}

2018-11-29

转载于:https://www.cnblogs.com/DWVictor/p/10036694.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值