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

最新文章

  1. WordPress基础:小工具的使用
  2. [Zigbee]定时器1
  3. oracle使用dbms_metadata包取得所有对象DDL语句
  4. hdu 4946 Area of Mushroom(凸包)
  5. slidingmenu
  6. 中国首个 SaaS 模式的云告警平台 iOS 版 APP 上线
  7. 通过 DevOps 整合开发和应用安全管道
  8. 海园帮忙写的JQUERY功能,实现了我们想要的,我觉得有点屌哟~~
  9. V9 二次开发技术篇之 模型数据库
  10. 用thinkphp开启伪静态,用wamp开启很快搞定;但是用phpstudy总是开启失败,为什么?
  11. 新人如何运行Faster RCNN的tensorflow代码
  12. 栈(LIFO)
  13. op 和 oo 的区别
  14. [笔记]_ELVE_正则表达式
  15. servlet中的请求响应与重定向区别
  16. Bootstrap3基础 input-group glyphicon 输入框组与glyphicon图标
  17. 【BZOJ3992】【SDOI2015】序列统计
  18. 【python系列】python初识
  19. eclipse中使用Maven新建Servlet2.5的Web项目
  20. redux学习与使用

热门文章

  1. SQL基础测试
  2. POJ 2186 挑战 --牛红人 强连通分量——Tarjan
  3. 【bzoj1189】[HNOI2007]紧急疏散evacuate
  4. R-CNN常见问题
  5. Spring Boot教程(十三)整合elk(2)
  6. Hbase数据备份&amp;&amp;容灾方案
  7. Yii2.0简单隐藏index.php文件和模块配置和layout布局配置禁用和日志写入配置
  8. 第三周syh
  9. 使用MingGW-w64 Build Script 3.6.7搭建ffmpeg编译环境
  10. zay大爷的神仙题目 D1T2-腐草为萤