PKU 2688

本文介绍了一个清洁机器人在布满障碍物的矩形房间内寻找最短路径以清洁所有污渍的问题。通过广度优先搜索算法计算从一个污渍点到另一个污渍点的最短距离,并采用深度优先搜索算法来找出清洁所有污渍所需的最小移动次数。

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

Cleaning Robot
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1728 Accepted: 738

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

#include<iostream>
#include<queue>
using namespace std;
#define inf 1000000000
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};//定义四个方向的数组
int n,m,l,res;
char map[21][21];
int grap[21][21];   //MAP是用来放图的 ,GRAP是用来记录长度的 (是脏点对脏点)
bool f[12];

struct _point
{
 int x,y,step;
 bool operator==(const _point& a) const  //定义==这个操作是X,Y相等
 {
  return x==a.x&&y==a.y;
 }
}s,e[12],temp;//定义一个结构体和操作 ,S用来记录机器人的位置,E是用来记录所有脏点,TEMP是交换用的

int dis(_point s,_point des)//广搜
{
 bool flag[21][21];
 int i;
 queue<_point> q;//SQL建立一个队列
 memset(flag,false,sizeof(flag));
 flag[s.x][s.y]=true;
 s.step=0;
 q.push(s);
 while(!q.empty())//当队列里还有点,因为使用这个来存序列
 {
  
  for(i=0;i<4;i++)
  {
   temp=q.front();
   temp.x+=dx[i];
   temp.y+=dy[i];
   if(flag[temp.x][temp.y]||map[temp.x][temp.y]=='x'||temp.x<1||temp.y<1||temp.x>n||temp.y>m) continue;
   temp.step++;
   flag[temp.x][temp.y]=true;
   q.push(temp);
   if(temp==des)
     return temp.step;
  }
  q.pop();
 }
 return -1;
}

void dfs(int t,int n,int pathlen)
{
 if(n>=l)
 {
  if(pathlen<res)
       res=pathlen;
  return;
 }
 int i;
 for(i=1;i<l;i++)
 {
        if( pathlen + grap[t][i] >= res ) continue;  //成功的剪枝
        if(!f[i]&&grap[t][i]!=inf)
  {
              f[i] = true;
              dfs(i, n+1, pathlen + grap[t][i]);
              f[i] = false;
        }
    }
}

int main()
{
 freopen("in.txt","r",stdin);
 freopen("out1.txt","w",stdout);
 int i,j,t;
    bool flag;
    while(scanf("%d%d",&m,&n),m||n)
    {
  getchar();//如果等下是一个一个字母的读入就需要把换行给去掉
  l=0;
  flag=false;
  for(i=0;i<=11;i++)
    for(j=0;j<=11;j++)  //脏点只有10个
      grap[i][j]=inf;
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=m;j++)
   {
    scanf("%c",&map[i][j]);
    if(map[i][j]=='o')
      s.x=i,s.y=j;   //如果是机器人现在的位置,就记录下来
    if(map[i][j]=='*')
      e[++l].x=i,e[l].y=j;//记录下每个不干净的点
   }
   getchar();
  }
  e[0]=s; //第一个点是机器人所在的点
  l++;
  for(i=0;i<l&&!flag;i++)
    for(j=0;j<l;j++)
    {
    if(i==j) continue;
    t=dis(e[i],e[j]);
    grap[i][j]=t;
    grap[j][i]=t;
    if(t==-1)
    {
     flag=true;
     break;
    }
     }//用BFS算出每个脏点的距离,以便得出结论,是否能都被清理干净
     if(flag)
     {
                printf("-1/n");
                continue;
     }
     res=(1<<25);//这个就是用位运算将RES定为最大值
     memset(f,0,sizeof(f));
     f[0]=true;
     dfs(0,1,0);
     printf("%d/n",res);
 }   
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值