Cleaning Robot (bfs+dfs)
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
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
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)的更多相关文章
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)
题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...
- HDU1254--推箱子(BFS+DFS)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- 图的基本遍历算法的实现(BFS & DFS)复习
#include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu1254(bfs+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 分析: 真正移动的是箱子,但是要移动箱子需要满足几个条件. 1.移动方向上没有障碍. 2.箱子后 ...
- 图的遍历(bfs+dfs)模板
bfs #include<iostream> #include<queue> #include<cstdio> using namespace std; queue ...
- UVA1600-Patrol Robot(BFS进阶)
Problem UVA1600-Patrol Robot Accept:529 Submit:4330 Time Limit: 3000 mSec Problem Description A rob ...
- 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 ...
随机推荐
- 如何修改 tomcat 端口号?
一.tomcat默认端口 tomcat默认的端口是8080,还会占用8005,8009和8443端口.如果已经启动了tomcat,再启动另一个tomcat就会发现 这些端口已经被占用了,这个时候就需要 ...
- XML 浏览器支持
几乎所有的主流浏览器均支持 XML 和 XSLT. Mozilla Firefox 从 1.0.2 版本开始,Firefox 就已开始支持 XML 和 XSLT(包括 CSS). Mozilla Mo ...
- zookeeper3.5.5集群部署
ZooKeeper是一个为分布式应用所设计的分布的.开源的协调服务,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,简化分布式应用协调及其管理的难度,提供高性能的分布式服务.ZooKeeper ...
- Java 内置锁 重入问题
阅读<Java并发编程实战>,遇到了一个问题 代码如下 /** * @auther draymonder */ public class Widget { public synchroni ...
- WinRAR 常用变量列表
%SystemDrive%操作系统所在的分区号.如 C:%SystemRoot%操作系统根目录.如 C:\WINDOWS%windir%操作系统根目录.如 C:\WINDOWS%ALLUSERSP ...
- 初识java虚拟机——JVM
1.Java程序运行过程 编写 编译 运行 过程如图所示: 2.JVM的认识 定义:JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚 ...
- POST上传多张图片配合Django接受多张图片
POST上传多张图片配合Django接受多张图片 本地:POST发送文件,使用的是files参数,将本地的图片以二进制的方式发送给服务器. 在这里 files=[("img",op ...
- listen( ) accept( )
服务器端,创建socket,bind绑定套接字后,还需要使用listen()函数让套接字进入被动监听状态,再调用accept()函数,就可以随时响应客户端的请求了 listen() 函数 通过 lis ...
- C#读取word文档中的内容
原文地址 http://blog.csdn.net/yhrun/article/details/7674540 在使用前需要添加引用巨硬的com组件:Microsoft Word 12.0 objec ...
- event.currentTarget
https://api.jquery.com/event.currentTarget/ event.currentTargetReturns: Element Description: The cur ...