poj 2688 cleaning robot(bfs+dfs)

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

给出一个地图,’.’表示地板,可到达,’x’表示家具,不可到达,’o’表示机器人的位置,’*’表示脏地板,

要求机器人清理完所有的脏地板,并且所走距离最小,如无解,输出-1。机器人从一块地板只能沿着上下左右的某一

方向走到另一块地板,且距离为1。

题目模型:

将机器人的位置和各个脏地板的位置看成一个图的各个顶点,那么题目的本质就是要求在一个无向图

中,遍历各个顶点至少一次,使得总路径最小。

经过分析后,可以发现此题即是典型的TSP问题,也就是旅行商问题或汉密尔顿通路。此题要求的就是

旅行商问题的最优值。


思路: 比赛的时候碰到这题两个小时都在搞这道题,以为一个bfs暴力就过了结果自己测试出好多样例错误就不知道怎

么改了;看了题解才知道是dfs+bfs

典型的acm开拓思维的题型,第一次遇见;

先bfs求出任意两点之间的最短距离,然后dfs求出哈密顿图的最短距离即可,

将机器人和脏地板看成顶点,先求出顶点两两之间的最短距离,此时问题便转换成了求解汉密尔顿通路的最优值,

因此只要从起点出发再进行一次DFS即可求出最优解即可。AC代码求解最短路径是用BFS的,因为这相当于一个迷宫,

直接BFS即可求出各顶点的最短路径。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x7fffffff
char map1[100][100];
int book[100][100];

int dis[100][100];

   queue<struct node>Q;

struct node
{
    int x;
    int y;
    int s;
} p[100],e,s;

int use[22];
int ans,a;
int bfs(int n,int m)///记录两两垃圾与机器人的最短距离
{
int next[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
   memset(book,0,sizeof(book));
       while(!Q.empty())
         Q.pop();
       int i;
       Q.push(s);
       while(!Q.empty())
        {    struct node l,g;
              g=Q.front();
              Q.pop();
              for(i=0;i<4;i++)
              {    l=g;
                   l.x+=next[i][0];
                   l.y+=next[i][1];
                   l.s++;
                  if(l.x<0||l.y<0||l.x>=m||l.y>=n||book[l.x][l.y]==1||map1[l.x][l.y]==‘x‘)
                    continue;
                    book[l.x][l.y]=1;
                   if(l.x==e.x&&l.y==e.y)

                       return l.s;

                     Q.push(l);
              }
        }
        return INF;

}

void dfs(int s,int num,int len)///搜索最短路径
{
     int i;
     if(num==0&&len<ans)
       {  ans=len;
          return ;
       }
       if(len>ans)
         return ;
        for(i=0;i<a;i++)
          {   if(!use[i]&&dis[s][i]!=INF)
               {   use[i]=1;
                    dfs(i,num-1,len+dis[s][i]);
                   use[i ]=0;
               }
          }
          return ;
}
int main()
{    int n,m,i,fa,j,st;

    while(scanf("%d%d",&n,&m)!=EOF)
    {

        getchar();
        if(n==0&&m==0)
            break;
      fa=a=0;
    memset(dis,0,sizeof(dis));

       for(i=0;i<m;i++)
       {
             scanf("%s",map1[i]);
               for(j=0;j<n;j++)
         {
                if(map1[i][j]==‘o‘||map1[i][j]==‘*‘)
                {
                    p[a].x=i;
                    p[a].y=j;

                  if(map1[i][j]==‘o‘)
                   {
                      st=a;
                   }
                    a++;
                }
         dis[i][j]=INF;
         }

       }

           for(i=0;i<a;i++)
           {
               for(j=i+1;j<a;j++)
               {
                   s=p[i];
                   e=p[j];
                   s.s=0;
                   dis[i][j]=dis[j][i]=bfs(n,m);

                   if(dis[i][j]==INF)
                   {
                       fa=1;
                       break;
                   }

                      }
           }
    if(fa)
    {
        printf("-1\n");
    }
    else
    {
              ans=INF;
                memset(use,0,sizeof(use));
                use[st]=1;
                dfs(st,a-1,0);
                printf("%d\n",ans);
    }

    }

}

原文地址:https://www.cnblogs.com/shuaihui520/p/8987483.html

时间: 2024-11-08 22:52:09

poj 2688 cleaning robot(bfs+dfs)的相关文章

POJ - 2688 Cleaning Robot

题意:求回收所有垃圾的最短路 思路:先BFS处理两个垃圾的距离,然后DFS记忆化搜索 dp[i][state]表示处理到第i个后状态是state的最短路 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int

poj 2688 Cleaning Robot (tsp问题)

Cleaning Robot Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4073   Accepted: 1659 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

POJ 1130(一道纯水,bfs+dfs)

POJ 1130 大概题意:给出一副图,求从起点到终点 (0->ET) 必须经过的一点. 我的思路:首先DFS求出经过每点的次数,必过的一点的次数一定最高,但是就这样吗?有可能有多个必过的点,所以还要找出离ET最近的点,这里就利用BFS逐层搜索的性质求每点到ET的距离. #include<iostream> #include<cstdio> #include<string.h> #include<iomanip> #include<queue&g

POJ 2907 Collecting Beepers (DFS+回溯)

Description Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in he

POJ 1856 Sea Battle(BFS).

~~~~ 题意: 给你一个R*C的图,求其由图中连通'#"所组成的矩形的个数. 注意:If the ships were placed correctly (i.e., there are only rectangles that do not touch each other even with a corner), print the sentence "There are S ships." where S is the number of ships. Otherwi

poj 2688 状态压缩dp解tsp

题意: 裸的tsp. 分析: 用bfs求出任意两点之间的距离后可以暴搜也可以用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发访问过s集合中的点,目前在点u走过的最短路程. 代码: //poj 2688 //sep9 #include <iostream> #include <queue> using namespace std; const int maxW=32; const int maxN=12; int dx[4]={-1,1,0,

POJ 1573:Robot Motion

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10267   Accepted: 5001 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in

邻结矩阵的建立和 BFS,DFS;;

邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!---------------------------------------------------------------------------------------------------------------------------------------//邻接矩阵的建立和 其BFS, DFS, 遍历 #include <

HDU ACM 1044 Collect More Jewels BFS+DFS

题意:在一个迷宫中,有一些宝物,从起点走到终点,问在给定的时间内,到达终点后所能拾取珠宝的最大价值. 分析(BFS+DFS): 1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大. BFS特点:对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元来存储状态) DFS特点:对于解决遍历和求所有问题有效,对于问