前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

作者头像
ShenduCC
发布2018-04-26 14:18:21
5510
发布2018-04-26 14:18:21
举报
文章被收录于专栏:算法修养算法修养

Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 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 Source

这道题目有很多解法吧,但是我觉得简单一点就是先BFS算出起点和每个脏的点之间最短距离,然后就是一个简单的TSP问题,用状态压缩DP就可以解决了。我是一遍过了,不免有点小激动呢

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>

using namespace std;
#define MAX 100000000
int dis[11][11];
int dis2[11];
char a[25][25];
int dp[1<<10][11];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int vis[25][25];
int st,ed;
bool res;
int n,m;
struct Node
{
    int x;
    int y;
    int num;
}b[11];
queue<Node> q;
int bfs(int x1,int y1,int x2,int y2)
{
    Node term1;
    term1.x=x1;term1.y=y1;term1.num=0;
    vis[x1][y1]=1;
    q.push(term1);
    while(!q.empty())
    {
        Node term=q.front();
        q.pop();
        if(term.x==x2&&term.y==y2)
        {
            return term.num;
        }
        for(int i=0;i<4;i++)
        {
            int xx=term.x+dir[i][0];
            int yy=term.y+dir[i][1];
            if(xx<1||xx>n||yy<1||yy>m)
                continue;
            if(a[xx][yy]=='x'||vis[xx][yy])
                continue;
            vis[xx][yy]=1;
            Node temp;temp.x=xx;temp.y=yy;temp.num=term.num+1;
            q.push(temp);
        }

    }
    return -1;
}
void init()
{
    memset(vis,0,sizeof(vis));
    while(!q.empty())
        q.pop();
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(n==0&&m==0)
            break;
        res=true;
        getchar();
        int cot=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%c",&a[i][j]);
                if(a[i][j]=='o'){st=i;ed=j;}
                else if(a[i][j]=='*'){b[cot].x=i;b[cot++].y=j;}
            }
            getchar();
        }
        for(int i=0;i<cot;i++)
            {
                init();dis2[i]=bfs(st,ed,b[i].x,b[i].y);
                if(dis2[i]==-1)
                {res=false;break;}
            }
        if(!res){printf("-1\n");continue;}
        for(int i=0;i<cot;i++)
            for(int j=i+1;j<cot;j++)
               {
                   init();
                   dis[i][j]=dis[j][i]=bfs(b[i].x,b[i].y,b[j].x,b[j].y);
               }
        int state=(1<<(cot))-1;
        for(int i=0;i<=state;i++)
            for(int j=0;j<cot;j++)
                dp[i][j]=MAX;
        for(int i=0;i<cot;i++)
            dp[1<<i][i]=dis2[i];
        for(int i=1;i<=state;i++)
        {
            for(int j=0;j<cot;j++)
            {
                if(!((1<<j)&i))
                    continue;
                for(int k=0;k<cot;k++)
                {
                    if(k==j) continue;
                    if((1<<k)&i) continue;
                    int ss=i+(1<<k);
                    dp[ss][k]=min(dp[ss][k],dp[i][j]+dis[j][k]);
                }
            }
        }
        int ans=MAX;
        for(int i=0;i<cot;i++)
            ans=min(ans,dp[state][i]);
        printf("%d\n",ans);
    }
        return 0;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档