POJ2688 Cleaning Robot
来源:互联网 发布:html中引入js的方式 编辑:程序博客网 时间:2024/05/17 20:33
Description
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.
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.
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
849-1
原来以为只是简单的bfs,直接写的,结果样例都出不来。了题解之后才发现应该是bfs+dfs,才想起某次比赛好像做过类似的题,bfs找任意两个要求到达点的最短路径,dfs查找最短路径。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;#define inf 0x3f3f3f3fstruct Node{ int x,y,step;}node[511];struct Node1{ int x,y;}target[15];int n,m,sum,sx,sy,flag,ans;char mp[21][21];bool vis[21][21],look[21];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int dis[15][15];queue<Node>q;int bfs(int x1,int y1,int x2,int y2){ memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); int i,j; Node now,next; now.x=x1;now.y=y1;now.step=0; q.push(now); vis[x1][y1]=1; while(!q.empty()) { now=q.front(); q.pop(); for(i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.step=now.step+1; if(next.x<0||next.x>=n||next.y<0||next.y>=m) continue; if(mp[next.x][next.y]=='x') continue; if(vis[next.x][next.y]) continue; if(next.x==x2&&next.y==y2) return next.step; else { vis[next.x][next.y]=1; q.push(next); } } } return inf;}void dfs(int x,int num,int len){ int i; if(len>ans) return; if(num==sum) { ans=(ans>len)?len:ans; return; } for(i=0;i<=sum;i++) { if(look[i]==0&&dis[x][i]!=inf) { look[i]=1; dfs(i,num+1,len+dis[x][i]); look[i]=0; } }}int main(){ while(~scanf("%d%d",&m,&n)) { int i,j; if(n+m==0) break; sum=0; memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) { scanf("%s",mp[i]); for(j=0;j<m;j++) { if(mp[i][j]=='o') { target[0].x=i;target[0].y=j; mp[i][j]='.'; } if(mp[i][j]=='*') { target[++sum].x=i;target[sum].y=j; } } } if(sum==0) { printf("0\n"); continue; } flag=1; int temp; for(i=0;i<=sum&&flag;i++) { for(j=i+1;j<=sum;j++) { temp=bfs(target[i].x,target[i].y,target[j].x,target[j].y); dis[i][j]=dis[j][i]=temp; if(temp==inf) { flag=0; break; } } } if(flag==0) { printf("-1\n"); continue; } ans=inf; memset(look,0,sizeof(look)); look[0]=1; dfs(0,0,0); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ2688 Cleaning Robot
- poj2688 Cleaning Robot (bfs+dfs)
- bfs+dfs- poj2688-Cleaning Robot
- poj2688 Cleaning Robot 简单BFS+DFS
- POJ2688 Cleaning Robot(BFS+TSP问题)
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
- Cleaning Robot
- poj 2688 Cleaning Robot
- POJ - 2688 Cleaning Robot
- poj 2688 Cleaning Robot
- POJ 2688 Cleaning Robot
- POJ 2688 Cleaning Robot
- Cleaning Robot development note
- Cleaning Robot development note
- poj 2688 Cleaning Robot
- poj2688
- PKU2688 Cleaning Robot(BFS + DFS)
- Cleaning Robot-dfs+bfs+tsp
- 让王者的梦想起航,vivo X20王者荣耀周年庆限量版美图来袭
- 凯文•凯利:预言科技的十大趋势,未来25年最重要的产品尚未出现
- 创建mongodb副本集集群(无用户认证)
- iPhone X 用户才能玩到的自拍新功能,背后原来是这些技术在支撑
- 陌陌走下约炮神坛,下一步该怎么走?
- POJ2688 Cleaning Robot
- 关于产品的一些交互理念
- Facebook伦敦新总部正式启用,2018年将新增800个工作岗位
- 华为前员工:我所知道的李一男
- 乌镇风云
- PPT之后 李彦宏用视频演示百度在人工智能领域的布局 | 乌镇峰会
- 博通强势公布11名提名董事,1300亿美元要约会否成为敌意收购?
- Android Go 终于来了,Google 想要靠它获取下一个 10 亿用户
- 我会说我喜欢创业嘛?(每个月总有几天会更新…………标题一定要长)