zoukankan      html  css  js  c++  java
  • Cleaning Robot (bfs+dfs)

    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
    ");
                continue;
            }
            memset(vist,0,sizeof(vist));
            vist[0]=1;
            ans=inf;
            dfs(0,0,0);
            printf("%d
    ",ans);
    
    
        }
    }

    2018-11-29

  • 相关阅读:
    2019春第十一周作业
    2019春第十周作业
    2019春第九周作业
    2019春第八周作业
    2019春第七周作业
    2019春第六周作业
    寒假作业一:打印沙漏
    寒假作业三:抓老鼠啊亏了~还是赚了?
    寒假作业二:币值转换
    秋季学期学习总结
  • 原文地址:https://www.cnblogs.com/DWVictor/p/10036694.html
Copyright © 2011-2022 走看看