POJ2688 Cleaning Robot(BFS)


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.

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,直接写的,结果样例都出不来。了题解之后才发现应该是bfs+dfs,才想起某次比赛好像做过类似的题,bfs找任意两个要求到达点的最短路径,dfs查找最短路径。
 
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #define inf 0x3f3f3f3f struct 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值