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. springboot 集成apollo,根据不同环境加载配置

  2. teradata安装

    一,下载 步骤1 - 从链接下载所需的VM版本,http://downloads.teradata.com/download/database/teradata-express-for-vmware- ...

  3. MoreExecutors工具类使用

    MoreExecutors是guava提供的工具类,是对jdk自带的Executors工具类的扩展,主要方法如下: 1.addDelayedShutDown()方法的两个重载: public stat ...

  4. 删除操作——str.subString(0,str.length()-1)

    subString是String的一个方法,格式为: public String substring(int beginIndex, int endIndex)  返回一个新字符串,它是此字符串的一个 ...

  5. Oracle报Ora01522

    应用服务报错截图 数据库后台日志报错截图 从日志分析应该是回滚异常造成表空间无法使用回滚段,而回滚涉及的表空间为undo表空间 尝试新建UNDO表空间,再将UNDO_TABLESPACE切换到新建的U ...

  6. [CSP-S模拟测试]:简单的括号序列(组合数)

    题目传送门(内部题82) 输入格式 一行一个字符串$ss$,保证$ss$中只包含$'('$和$')'$. 输出格式 一行一个整数,表示满足要求的子序列数对$10^9+7$的结果. 样例 样例输入1: ...

  7. @清晰掉 sprintf sscanf双胞胎

    sprintf() 格式化输出函数(图形) 功能: 函数sprintf()用来作格式化的输出.用法: 此函数调用方式为int sprintf(char *string,char *format,arg ...

  8. jquery.fileupload-image-editor.js

    jquery.fileupload-image-editor.js中 _initEventHandlers: function () { this._super(); var handlers = { ...

  9. 【零售App】—— react/ant design mobile项目爬坑

    一.H5制作 - 图片文本的动画效果 bug:打开一个模板,添加图片,添加动画效果,若先选定动画效果,再调节动画时间和延迟时间,则动画和延迟时间没有改变:若先调节动画时间和延迟时间在选定动画效果,则动 ...

  10. leetcode-mid-others-169. Majority Element¶

    mycode  54.93% class Solution(object): def majorityElement(self, nums): """ :type num ...