Cleaning Robot

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wastematerial/article/details/46670173
Description

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

题意:

给你一个N * M(1 ≤ N, M ≤ 20)的地图,'.'表示空位,'*'表示垃圾,'o'表示机器人的初始位置,'x'表示墙。请你计算机器人清理完所有的垃圾至少要走多少步。

机器人一旦走到垃圾上,垃圾就被清掉了。输入数据保证垃圾不多于10个。
思路:把每两个点的最小距离用BFS求出来,变成近似tsp问题,然后用dfs进行求出最优路线;
贴代码:
 
  
#include <stdio.h>
#include <string.h>
#include <queue>
#define MAX 0x3f3f3f3f
using namespace std;
char gp[25][25];
int n,m,sum,v,ans;
int dis[25][25];//邻接矩阵求出每两个点的距离
int book[25][25];
struct nd
{
    int x;
    int y;
}jd[20];//记录有多少个要走的点
struct node
{
    int x;
    int y;
    int step;
    node(int xx,int yy,int st)
    {
        x=xx;
        y=yy;
        step=st;
    }
    friend bool operator < (const node ss,const node qq)
    {
        return ss.step>qq.step;
    }
};
priority_queue <node> q;
int bfs(const nd &qd,const nd &zd)//广搜两点的最短距离
{
    int fx[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
    while(!q.empty())
        q.pop();
    q.push(node(qd.x,qd.y,0));
    int vist[20][20];
    memset(vist,0,sizeof(vist));
    vist[qd.x][qd.y]=1;
    while(!q.empty())
    {
        node cl=q.top();
        q.pop();
       // printf("  %d %d %d %d\n",cl.x,cl.y,cl.step,cl.gs);
        if(cl.x==zd.x&&cl.y==zd.y)
            return cl.step;
        for(int i=0;i<4;i++)
        {
            int zx,zy;
            zx=cl.x+fx[i][0];
            zy=cl.y+fx[i][1];
            if(zx<0||zx>=n||zy<0||zy>=m)
                continue;
            if(gp[zx][zy]!='x'&&vist[zx][zy]==0)
            {
                vist[zx][zy]=1;
                q.push(node(zx,zy,cl.step+1));
            }
        }
    }
    return -1;
}
void dfs(int qs,int anns,int sum)//深搜最短路线
{
    if(sum==v)
    {
        if(anns<ans)
            ans=anns;
        return ;
    }
    book[jd[qs].x][jd[qs].y]=1;
    for(int i=0;i<=v;i++)
    {
        if(anns+dis[qs][i]>=ans)//重要的剪枝,不加会超时
            continue;
        if(book[jd[i].x][jd[i].y]==0)
           dfs(i,anns+dis[qs][i],sum+1);
    }
    book[jd[qs].x][jd[qs].y]=0;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(m==0&&n==0)
            break;
        v=0;
        int flag=0;
        int sx,sy;
        memset(dis,0,sizeof(dis));
        for(int i=0;i<n;i++)
        {
             scanf("%s",gp[i]);
             for(int j=0;j<m;j++)
             {
                 if(gp[i][j]=='o')
               {
                      sx=i;
                      sy=j;
                      jd[0].x=i;//起点为第一个要走的点
                      jd[0].y=j;
                }
                 if(gp[i][j]=='*')
                {
                      v++;
                      jd[v].x=i;
                      jd[v].y=j;
                 }
             }
        }
        for(int i=0;i<=v;i++)
            for(int j=0;j<=v;j++)
          {
              if(j>=i)
                break;
            if(dis[i][j]==0)
            {
                 dis[i][j]=dis[j][i]=bfs(jd[i],jd[j]);//求出每两个点的最短距离
                 if(dis[i][j]==-1)//走不通
                 {
                      flag=1;
                      break;
                 }
            }
          }
        if(flag==1)
        {
            printf("-1\n");
            continue;
        }
        ans=MAX;
        memset(book,0,sizeof(book));
        dfs(0,0,0);
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/Wastematerial/article/details/46670173