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[][];
int dis[][];
int vis[][];
int tag[][];
const int inf = ;
struct node
{
int x,y,step;
} point[]; int w,h,cnt,ans;
void bfs(node fir,int pt)//通过bfs来记录下所有的点的位置
{
queue <node>s;
fir.step=;
while(!s.empty())
s.pop();
vis[fir.x][fir.y]=;
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[][]={,,,-,,,-,};
for(int i=;i<;i++)
{
node temp = t;
temp.x+=next[i][];
temp.y+=next[i][];
if(temp.x<||temp.y<||temp.x>h||temp.y>w||vis[temp.x][temp.y]==||mp[temp.x][temp.y]=='x')
{
continue;
}
temp.step+=;
s.push(temp);
vis[temp.x][temp.y]=;
}
}
}
int vist[];
void dfs(int x,int step,int s)
{
if(step==cnt)
{
ans=min(s,ans);
return ;
}
if(s>ans)
return ;
for(int i=;i<=cnt;i++)
{
if(vist[i])
continue ;
vist[i]=;
dfs(i,step+,s+dis[x][i]);
vist[i]=;
}
}
int main()
{
while(scanf("%d%d",&w,&h))
{
if(w==&&h==)
break;
cnt = ;
getchar();
memset(point,,sizeof(point));
memset(tag,,sizeof(tag));
memset(dis,,sizeof(dis));
for(int i=;i<=h;i++)
{
for(int j=;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]=;
point[].x=i;
point[].y=j;
}
}
getchar();
}
for(int i=;i<=cnt;i++)
{
for(int j=;j<=cnt;j++)
{
if(i!=j)
dis[i][j]=inf;
else
dis[i][j]=;
}
}
for(int i=;i<=cnt;i++)
{
memset(vis,,sizeof(vis));
bfs( point[i],i );
} bool flag=;
for(int i=;i<=cnt && flag;i++)
for(int j=;j<=cnt && flag;j++)
if(dis[i][j]==inf)
flag=;
if(!flag)
{
printf("-1\n");
continue;
}
memset(vist,,sizeof(vist));
vist[]=;
ans=inf;
dfs(,,);
printf("%d\n",ans); }
}

2018-11-29

Cleaning Robot (bfs+dfs)的更多相关文章

  1. HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

    Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...

  2. hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)

    题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...

  3. HDU1254--推箱子(BFS+DFS)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  4. 图的基本遍历算法的实现(BFS & DFS)复习

    #include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...

  5. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. hdu1254(bfs+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 分析: 真正移动的是箱子,但是要移动箱子需要满足几个条件. 1.移动方向上没有障碍. 2.箱子后 ...

  7. 图的遍历(bfs+dfs)模板

    bfs #include<iostream> #include<queue> #include<cstdio> using namespace std; queue ...

  8. UVA1600-Patrol Robot(BFS进阶)

    Problem UVA1600-Patrol Robot Accept:529  Submit:4330 Time Limit: 3000 mSec Problem Description A rob ...

  9. POJ-3083 Children of the Candy Corn (BFS+DFS)

    Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...

随机推荐

  1. java.lang.ClassNotFoundException: org.springframework.web.util.WebAppRootListener

    严重: Error configuring application listener of class org.springframework.web.util.WebAppRootListenerj ...

  2. ideal 工具jdk环境配置

    1.File  >>  Other Settings >> Default Project Structure ... 2.Project  >>  jdk_vie ...

  3. PHP大文件分片上传断点续传实例源码

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  4. 视图:setContentView()

    1.setContentView的作用是将View加载到根view之上,这样当显示view时,先显示根view,然后在显示子view,以此类推,最终将所有view显示出来. 2.setContentV ...

  5. tomcat8 的优化

    1.下载tomcat8 2.配置 修改tomcat_user.xml,配置管理用户(设置角色,和用户密码) <role rolename="manager"/> < ...

  6. 阿里云OSS文件上传封装

    1.先用composer安装阿里云OSS的PHPSDK 2.配置文件里定义阿里云OSS的秘钥 3.在index控制器里的代码封装 <?php namespace app\index\contro ...

  7. serlvet操作数据库

    工具:eclipse 数据库工具:mysql java ee操作数据库,首先要导入数据库驱动文件,我用的是mysql 刚开始,很多人代码正确但是就是连接不上,原因就是忘了驱动文件的导入. 我的驱动文件 ...

  8. linux下插入U盘自动挂载后,用C获取其挂载点(cat /proc/mounts)

    现在已经能够通过libudev获取U盘插入时它的节点名(通过函数udev_device_get_devnode()),是/dev/sdb1 我现在的做法是读取/proc/mounts文件,找到有/de ...

  9. nginx location的优先级

    原来一直以为location的优先级是先后顺序,结果有次项目中傻眼了,赶紧百度一下,下面的内容参考了这个链接 location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配 ...

  10. Spring+hibernate 配置实例

    转自:http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html 项目结构: http://www.cnb ...