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. ArrayList的contains()和HashSet的contains()效率比较

    ArrayList的contains(Object o)方法内部只有一行代码:判断indexOf(0)是否大于等于0.而indexOf(o)内部会从头遍历数组,直到某位置的元素等于o,极端情况下,要把 ...

  2. 【canvas学习笔记五】使用图片

    在canvas里画图有两个步骤: 获得图片源. drawImage()画图. 图片源 canvas支持以下几种图片资源: HTMLImageElement 可以使用Image()方法构造的图片,也可以 ...

  3. springboot(一).初识springboot以及基本项目搭建

    初识springboot 以及基本项目搭建 由于新的项目需要搭建后台框架,之前的springmvc架构也使用多次,在我印象中springboot的微服务架构更轻量级更容易搭建,所以想去试试spring ...

  4. 3D Computer Grapihcs Using OpenGL - 13 优化矩阵

    上节说过矩阵是可以结合的,而且相乘是按照和应用顺序相反的顺序进行的.我们之前初始化translationMatrix和rotationMatrix的时候,第一个参数都是使用的一个初始矩阵 glm::m ...

  5. 七、chromedriver各版本下载网址

    http://chromedriver.storage.googleapis.com/index.html

  6. 2018-5 - 凉经 - 乐糖游戏 - PHP 开发实习生

    收到面试通知当天因为学校出事要求我明天必须回去,所以就买当晚的火车票,然后跟公司说学校有事明天没法去面试了,公司人事比较好给我安排到当天下午面试.公司规模不是很大,但位置好下了地铁到,可能因为招的是实 ...

  7. lua源码学习篇四:字节码指令

    在llimits.h文件中定义了指令的类型.其实就是32个字节. typedef lu_int32 Instruction; 上节说到变量最终会存入proto的数组k中,返回的索引放在expdesc ...

  8. Spring Cloud的几个组件

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...

  9. 【命令汇总】Windows 应急响应

    日期:2019-06-07 16:11:49 作者:Bay0net 介绍:Windows 应急响应.取证及溯源相关内容学习记录 0x00.前言 常见的应急分类: web入侵:网页挂马.主页篡改.Web ...

  10. Centos 安装 FTP

    安装教程:   基于 CentOS 搭建 FTP 文件服务 搭建完成后,使用windows文件夹访问FTP报错,请确保输入的文件名是否正确,并且您有权访问此文件. 先确认ftp服务正常 修改:   设 ...