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.

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

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;}